Convert Figma logo to code with AI

dotnet logoroslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.

18,892
4,011
18,892
9,327

Top Related Projects

100,112

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

48,780

The Kotlin Programming Language.

67,285

The Swift Programming Language

96,644

Empowering everyone to build reliable and efficient software.

22,079

Adds static typing to JavaScript to improve developer productivity and code quality.

Quick Overview

Roslyn is the open-source implementation of the C# and Visual Basic compilers, providing rich code analysis APIs. It's the foundation for .NET compiler platform services, offering powerful tools for code analysis, refactoring, and generation.

Pros

  • Provides a powerful set of APIs for code analysis and manipulation
  • Enables creation of custom analyzers and code fixes
  • Supports both C# and Visual Basic languages
  • Continuously updated to support the latest language features

Cons

  • Steep learning curve for newcomers to compiler technology
  • Large codebase can be overwhelming for contributors
  • Performance can be an issue for large-scale analysis tasks
  • Documentation, while improving, can sometimes lag behind new features

Code Examples

  1. Creating a syntax tree from source code:
using Microsoft.CodeAnalysis.CSharp;

var tree = CSharpSyntaxTree.ParseText(@"
    class Program
    {
        static void Main()
        {
            System.Console.WriteLine(""Hello, World!"");
        }
    }
");
  1. Finding all method declarations in a syntax tree:
using Microsoft.CodeAnalysis.CSharp.Syntax;

var root = tree.GetCompilationUnitRoot();
var methods = root.DescendantNodes()
    .OfType<MethodDeclarationSyntax>();

foreach (var method in methods)
{
    Console.WriteLine($"Method: {method.Identifier}");
}
  1. Creating a custom diagnostic analyzer:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MyAnalyzer : DiagnosticAnalyzer
{
    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; }
        = ImmutableArray.Create(Descriptor);

    public override void Initialize(AnalysisContext context)
    {
        context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.MethodDeclaration);
    }

    private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
    {
        // Implement your analysis logic here
    }
}

Getting Started

To get started with Roslyn, follow these steps:

  1. Install the .NET SDK
  2. Create a new console application:
    dotnet new console -n RoslynExample
    
  3. Add the necessary NuGet packages:
    dotnet add package Microsoft.CodeAnalysis.CSharp
    
  4. Use the Roslyn APIs in your code (see examples above)
  5. Run your application:
    dotnet run
    

For more detailed information and advanced usage, refer to the official Roslyn documentation and samples in the GitHub repository.

Competitor Comparisons

100,112

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

Pros of TypeScript

  • Broader ecosystem and community support, with extensive npm package availability
  • Easier learning curve for developers familiar with JavaScript
  • More flexible type system, allowing for gradual typing and easier migration from JavaScript

Cons of TypeScript

  • Less comprehensive tooling for advanced code analysis and refactoring
  • Slower compilation times for large projects
  • Limited support for metaprogramming compared to Roslyn's capabilities

Code Comparison

TypeScript:

interface Person {
  name: string;
  age: number;
}

function greet(person: Person) {
  console.log(`Hello, ${person.name}!`);
}

Roslyn (C#):

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public void Greet(Person person)
{
    Console.WriteLine($"Hello, {person.Name}!");
}

Both examples demonstrate similar syntax for defining types and using string interpolation. TypeScript's interface is more lightweight, while C# uses a class with properties. The function/method declarations are also similar, with TypeScript using type annotations and C# using explicit type declarations.

48,780

The Kotlin Programming Language.

Pros of Kotlin

  • More concise syntax, reducing boilerplate code
  • Better null safety with built-in null-checking mechanisms
  • Seamless interoperability with Java, allowing gradual adoption

Cons of Kotlin

  • Smaller community and ecosystem compared to C#
  • Slower compilation times, especially for large projects
  • Less mature tooling and IDE support outside of IntelliJ IDEA

Code Comparison

Kotlin:

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val doubled = numbers.map { it * 2 }
    println(doubled)
}

C# (Roslyn):

class Program {
    static void Main() {
        var numbers = new List<int> { 1, 2, 3, 4, 5 };
        var doubled = numbers.Select(x => x * 2);
        Console.WriteLine(string.Join(", ", doubled));
    }
}

Both Kotlin and Roslyn are powerful compiler projects, but they serve different purposes. Kotlin is a modern programming language designed to be more expressive and concise than Java, while Roslyn is a compiler platform for C# and Visual Basic that enables rich code analysis and transformation tools.

Kotlin offers a more streamlined syntax and better null safety, making it attractive for Android development and as a Java alternative. Roslyn, on the other hand, provides a robust foundation for .NET development and tooling, with a larger ecosystem and more mature IDE support across various platforms.

67,285

The Swift Programming Language

Pros of Swift

  • Open-source development with a more community-driven approach
  • Designed for modern, safe programming with features like optionals and value types
  • Faster compilation times for large projects

Cons of Swift

  • Smaller ecosystem and fewer third-party libraries compared to .NET
  • Less mature tooling and IDE support outside of Apple's ecosystem
  • More frequent language changes, potentially leading to compatibility issues

Code Comparison

Swift:

let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 }
print(doubled)

Roslyn (C#):

var numbers = new[] { 1, 2, 3, 4, 5 };
var doubled = numbers.Select(x => x * 2);
Console.WriteLine(string.Join(", ", doubled));

Summary

Both Swift and Roslyn are powerful compiler projects, but they serve different purposes. Swift focuses on being a modern, safe language for Apple platforms and beyond, while Roslyn provides a comprehensive set of compiler services for .NET languages. Swift offers a more streamlined syntax and faster compilation for large projects, but Roslyn benefits from the extensive .NET ecosystem and more stable tooling. The choice between them often depends on the target platform and specific project requirements.

96,644

Empowering everyone to build reliable and efficient software.

Pros of Rust

  • Memory safety without garbage collection, leading to better performance
  • Powerful type system and ownership model for preventing common programming errors
  • Growing ecosystem with a focus on systems programming and low-level control

Cons of Rust

  • Steeper learning curve due to unique concepts like ownership and lifetimes
  • Smaller community and ecosystem compared to C# and .NET
  • Longer compilation times, especially for large projects

Code Comparison

Rust:

fn main() {
    let x = 5;
    println!("The value of x is: {}", x);
}

Roslyn (C#):

class Program {
    static void Main() {
        int x = 5;
        Console.WriteLine($"The value of x is: {x}");
    }
}

Summary

Rust and Roslyn represent different approaches to modern programming languages. Rust focuses on systems programming with memory safety and performance, while Roslyn (C#) offers a more traditional object-oriented approach with garbage collection. Rust's unique features provide strong guarantees but come with a steeper learning curve. Roslyn benefits from a larger ecosystem and easier adoption for developers familiar with C-style languages. Both projects are open-source and actively maintained, with strong communities supporting their development.

22,079

Adds static typing to JavaScript to improve developer productivity and code quality.

Pros of Flow

  • Designed specifically for JavaScript, providing type checking tailored to JS ecosystem
  • Lighter weight and faster to set up compared to Roslyn's more comprehensive approach
  • Integrates well with popular JS tools and frameworks like React

Cons of Flow

  • Limited to JavaScript/TypeScript, while Roslyn supports multiple .NET languages
  • Smaller community and ecosystem compared to Roslyn's extensive Microsoft backing
  • Less powerful in terms of code analysis and refactoring capabilities

Code Comparison

Flow:

// @flow
function add(x: number, y: number): number {
  return x + y;
}

Roslyn:

public int Add(int x, int y)
{
    return x + y;
}

Summary

Flow is a static type checker for JavaScript, while Roslyn is a more comprehensive compiler platform for .NET languages. Flow offers lightweight type checking specific to JavaScript, making it easier to adopt in JS projects. However, Roslyn provides more robust analysis and supports multiple languages, backed by Microsoft's resources. The choice between them depends on the specific language and ecosystem requirements of a project.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Roslyn logo

The .NET Compiler Platform

Roslyn is the open-source implementation of both the C# and Visual Basic compilers with an API surface for building code analysis tools.

C# and Visual Basic Language Feature Suggestions

If you want to suggest a new feature for the C# or Visual Basic languages go here:

Contributing

All work on the C# and Visual Basic compiler happens directly on GitHub. Both core team members and external contributors send pull requests which go through the same review process.

If you are interested in fixing issues and contributing directly to the code base, a great way to get started is to ask some questions on GitHub Discussions! Then check out our contributing guide which covers the following:

Community

The Roslyn community can be found on GitHub Discussions, where you can ask questions, voice ideas, and share your projects.

To chat with other community members, you can join the Roslyn Discord or Gitter.

Our Code of Conduct applies to all Roslyn community channels and has adopted the .NET Foundation Code of Conduct.

Documentation

Visit Roslyn Architecture Overview to get started with Roslyn’s API’s.

NuGet Feeds

The latest pre-release builds are available from the following public NuGet feeds:

  • Compiler: https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json
  • IDE Services: https://pkgs.dev.azure.com/azure-public/vside/_packaging/vssdk/nuget/v3/index.json
  • .NET SDK: https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json

Continuous Integration status

Builds

BranchWindows DebugWindows ReleaseUnix Debug
mainBuild StatusBuild StatusBuild Status

Desktop Unit Tests

BranchDebug x86Debug x64Release x86Release x64
mainBuild StatusBuild StatusBuild StatusBuild Status

CoreClr Unit Tests

BranchWindows DebugWindows ReleaseLinux
mainBuild StatusBuild StatusBuild Status

Integration Tests

BranchDebug x86Debug x64Release x86Release x64
mainBuild StatusBuild StatusBuild StatusBuild Status

Misc Tests

BranchDeterminismAnalyzersBuild CorrectnessSource buildTODO/PrototypeSpanishMacOS
mainBuild StatusBuild StatusBuild StatusBuild StatusBuild StatusBuild StatusBuild Status

.NET Foundation

This project is part of the .NET Foundation along with other projects like the .NET Runtime.