Getting Started with Roslyn

Introduction

Welcome to today's article. Today, I would like to introduce you to the .NET Compiler Platform, known as Roslyn.

Roslyn

Roslyn is the codename for the .NET Compiler Platform. This platform is a set of code analysis APIs and open-source compilers for Visual Basic.Net and C#. Roslyn enables building code analysis tools with the same APIs that are used by Visual Studio.

As quoted from Roslyn's Wikipage:

The .NET Compiler Platform ("Roslyn") exposes a set of Compiler APIs and Workspaces APIs that provides rich information about your source code and that has full fidelity with the C# and Visual Basic languages. The transition to compilers as a platform dramatically lowers the barrier to entry for creating code focused tools and applications. It creates many opportunities for innovation in areas such as meta-programming, code generation and transformation, interactive use of the C# and VB languages, and embedding of C# and VB in domain specific languages.

Installing Roslyn

Step 1: Download the latest version of Visual Studio. In a later article, I will explain in detail how to do this.

Step 2: Roslyn can be found on GitHub. Simply download the zip file to a specified location on your hard disk. Once the download has completed, extract it to a folder of your choice. It unfortunately takes a while because the zip file is quite big.

Step 3: Depending on your version of Visual Studio, you have to download its associated SDK. In my case, I have Visual Studio 2015 RC installed.

Roslyn1
Figure 1: Installing Visual Studio 2015 RC SDK

Step 4: Install the SDK Templates VSIX package.

Roslyn2
Figure 2: Installing .NET Compiler Platform SDK Templates

Step 5: Install the Syntax Visualizer VSIX package.


Figure 3: Roslyn3

Roslyn should now be installed. You may also want to have a look into the zip file's contents that have unzipped earlier. Inside, you will find the samples under the docs folder as well as Roslyn.sln and RoslynLight.sln. RoslynLight.sln represents the source code buildable in the latest public release. Roslyn.sln represents all of the source code.

When you open Visual Studio RC, you will immediately see the Roslyn improvements, which include the following:

'#Region' Directives Inside Method Bodies

Another feature that I have begged for a long time to have is the ability to include a Region inside a method. Here is how you would be able to implement it:

Function ReturnName(strName As String, _
   strSurname As String)
Try
'Method Body here
   #Region "In case of Error"
   Catch ex As Exception
   'Error code here
   #End Region
End Function

The ?. Operator

Having to check for NULL values, or values that are Nothing, has always been a pain for me, and I believe many a fellow VB. Developer. This operator makes it easy to check for null values in objects. Here is an example of its implementation:

'Old
Dim EmpObject = Employee.Name
Dim Result = If(EmpObject IsNot Nothing, _
   Employee.Name.Surname, Nothing)
'New
Dim x = Employee.Name?.Surname

Multiline String Literals

This is not my favorite feature, but it is new. You will be able to split string literals over multiple lines. Here is an example:

Dim strStringVariable = "Hello
Hannes!
How
are
you
today?"

Now, for a guy who has used VB since forever, I am not entirely happy with this. I still love my concatenation....

String Interpolation

Now, this I love!

We, as VB developers, have been accustomed to using expression placeholders in our strings. With String Interpolation, using an expression inside a string has become much easier. Here is an example:

'string with expressions
Dim strName = String.Format("hello {0} {1}", _
   strName, strSurname)
'string interpolation
Dim s = $"hello {strName} {strSurname}"

Comments Allowed in More Places

In olden days, it was impossible to add a comment inside a LINQ expression and in-line continuations. Now, this is impossible no more because the following will be allowed:

Dim arrEmployees = {"Hannes",   'Me
   "Ella",                      'My wife
   "Kay"                        'My daughter
}

'loop through employees
Dim EmployeeAdresses = From i In arrEmployees
   Let EmployeeAdress = Lookup(i)   'lookup
   Select i, EmployeeAdress

Conclusion

Now that you know how to install Roslyn and how it works, you can explore the world of Roslyn further by keeping a close eye on GitHub for more improvements. Until we meet again.



Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date