Paging Doctor Roslyn!

Ever thought "Wow! It would be cool to be analyze code & get some feedback on it." Then you think "Wow! That would be insanely hard & complicated". Look no further. We now have Roslyn or better known as the .Net Compiler Platform. So where to start.

First, we need to get the Nuget package that will allow us to use the Roslyn APIs to do some code analysis & return diagnostics. You can either use the NuGet Package manager (below picture) or from the Package Manager Console & issue the following command: Install-Package Microsoft.CodeAnalysis.

Now that we have the package installed we need to include some libraries in our application. For this demo I'm just using a standard Console Application. You only need one using for Microsoft.CodeAnalysis.CSharp in your Program.cs file & you are ready to rock & roll. Below is the code to analyze a string & print out diagnostics

using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;
using System.Linq;
namespace CodeAnalyzer
    class Program
        static void Main(string[] args)
            var tree = CSharpSyntaxTree.ParseText(@"
                public void DoSomething()
            var results = tree.GetDiagnostics().Where(x => x.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error);
Seriously that is it. Simply create a syntax tree & get diagnostics. The compiler takes care of everything. Now in this case we are only grabbing the first error. Without the FirstOrDefault() we would get a collection (IEnumerable<Diagnostic>) of results. You can see that the above method does not have a closing bracket. So let's run the program & see what we get.
Now ain't that cool! The cool part is you could pass in any string to the ParseText() method for analysis. Say you have an ASP.Net MVC app that someone can insert custom logic. Simply call a method that has a string overload from an Controller & return the results to the user. 

Now if you want to see a pretty awesome example of using Roslyn, go to This is a plugin for Visual Studio that utilized Roslyn for real time code analysis without debugging or running the program. I'l let you see for youself.

Happy coding!

C# Extension Methods & Cleaner Code

My goal in this post is to explain & show you what Extension Methods are and why they are important. I also hope that this will allow you to write cleaner more understandable code for you & others.

So what are Extension Methods? Extension Methods "simply" allow you to tack on methods to .Net types such as strings or integers to act on them. Now when I say "tack on" I mean you can use the "." access modifier to access common functionality that you or someone else has written. If you want the Microsoft MSDN definition see this link. So what does an Extension Method look like? Take a look below:

The Extension Method in the above example is RemoveNumbers();. I'd like to make a point that Extension Methods should easily identify what is going to be done with the type it is being used with. In this case we can come to the conclusion that numbers will be removed from the string testString. Great, we now know what Extension Methods look like when being used but how to you create one. Let's take a look at the code below:

An extension method is defined just like any other method. As you can see this method is public & static. It also returns a string. The method overload is what defines what type the Extension Method will work with. The this keyword before String tells .Net that this method is to be used with the String type. So any variable that is of type String can use this extension method. Nice! Now if you read the above Extension Method you will see that it is simply taking a string & removing numbers...plain & simple. Now remember Extension Methods can do what ever you want them to....with in the confines of .Net & C# of course. 

Now lets say your are developing a library to work with Twitter and you are going to make it public by pushing it to Github. Making your library easy to use is going to help make your library successful. 

A user's goal is to get handles from a list of users on Twitter. Normally you would instantiate a class, call a method pass in values and then get your results. The code is going to look a little ugly and not feel very intuitive. With extension methods a user could simply do myUsers.GetHandle();. The user can now in one line of code do work on their list of users and quickly get the results they want. All the messiness of the logic is hidden from the user. 

As I said before naming is also key to extension methods. You want the name to quickly identify what is going to happen. When a user hits the "." after an typing in an object Intellisense is going to give them a long list of options. Extension method names will play a key part in how fast a user can find and identify the correct method they need. 

I hope this helps you in your development career and also shows you that Extension Methods allow you and others to quickly act on objects in .Net. One more tool in the toolbox. Happy coding!