See What's New in C# 6 and Visual Studio 2015

The future versions are C# 6.0, .NET 4.6, Visual Studio 2015. This article talks about the great things that are coming our way.

From the official project page, Roslyn on Github: C# 6.0 Features.

From the MSDN Blog, IDE features.

C# 6.0

Null Conditional Operator

I bet you have loads of null checks in your code preceding attempts to call a method on instances. With this new operator this becomes so much easier and clean.

For example, if you have a structure like this:

  1. public class Sample    
  2. {    
  3.     public Foo FooProperty { getset; }    
  4. }    
  5.      
  6. public class Foo    
  7. {    
  8.     public Bar BarProperty { getset; }    
  9. }    
  10.      
  11. public class Bar    
  12. {    
  13.     public void DoSomething()    
  14.     {    
  15.         Console.WriteLine("Doing something");    
  16.     }    
  17. }  
And you want to call Bar.DoSomething you would need to check if the instances are not null until you get there:
  1. static void Main(string[] args)    
  2. {    
  3.     Sample nullSample = null;    
  4.      
  5.     if (nullSample != null    
  6.         && nullSample.FooProperty != null    
  7.         && nullSample.FooProperty.BarProperty != null)    
  8.     {    
  9.         nullSample.FooProperty.BarProperty.DoSomething();    
  10.     }    
  11. }   
With this new null conditional operator you can simplify to this:
  1. static void Main(string[] args)    
  2. {    
  3.     Sample nullSample = null;    
  4.      
  5.     nullSample?.FooProperty?.BarProperty?.DoSomething();    
  6. }   
Auto-Property Initializers

Sometimes you need to instantiate a class and set some default values to some of the properties, when you need that you often do it in the constructor.
  1. public class Sample    
  2. {    
  3.     public Sample()    
  4.     {    
  5.         Name = "Default initial value";    
  6.     }    
  7.     public string Name { getset; }    
  8. }   
With the Auto-Property Initializer your can do that without having to set it on the constructor.
  1. public class Sample    
  2. {    
  3.     public string Name { getset; } = "Default initial value";    
  4. }   
Primary Constructors (not in this release)

Not planned for the first release, but they didn't discard the idea. Primary constructors allows you to basically have a class with parameters.
  1. public class Sample(string name)    
  2. {    
  3.     public string Name { getset; } = name;    
  4. }  
nameof Expressions

The nameof expression lets you grab the name of the variable you are working with. It can be used in the NotifyPropertyChanged method, for example:
  1. public class Sample : INotifyPropertyChanged    
  2. {    
  3.     private string name;    
  4.      
  5.     public string Name    
  6.     {    
  7.         get { return name; }    
  8.         set    
  9.         {    
  10.             name = value;    
  11.             NotifyPropertyChanged(nameof(Name));    
  12.         }    
  13.     }    
  14.      
  15.     public void NotifyPropertyChanged(string propName)    
  16.     {    
  17.         if (PropertyChanged != null)    
  18.             PropertyChanged(this,    
  19.                 new PropertyChangedEventArgs(propName));    
  20.     }    
  21.      
  22.     public event PropertyChangedEventHandler PropertyChanged;    
  23. }  
It could also be used on an exception:
  1. public void DoSomething(object someParam)    
  2. {    
  3.     if (someParam == null)    
  4.     {    
  5.         throw new ArgumentNullException(nameof(someParam));    
  6.     }    
  7.     //...    
  8. }   
So rather than hard-coding magic strings you can use this expression.

String interpolation

You often must write:
  1. String.Format("hello {0}", sample.Name);   
And the code might require new parameters so you need to put more numbers and always keep them in the correct order. It can grow and become very confusing. To solve this problem they are introducing the string interpolation that allows us to add parameter values directly in the string.

To do that you must prefix your string with the $ symbol.
  1. String.Format($"hello {sample.Name}");  
Expression-bodied Members

You can define method bodies using lambdas.
  1. public class Sample    
  2. {    
  3.     public string Name { get; } = "Foo";    
  4.      
  5.     public override string ToString() => Name;    
  6. }   
Exception filters

You can choose when to catch an exception, for example:
  1. public void DoSomething()    
  2. {    
  3.     bool exceptionCatchingEnabled = true;    
  4.      
  5.     try    
  6.     {    
  7.     }    
  8.     catch (Exception) when (exceptionCatchingEnabled)    
  9.     {    
  10.     }    
  11. }   
Visual Studio 2015

Improved ToolTips

Now tooltips include colors, glyph and more information.

tooltips

Improved ToolTips

Code Fixes and Refactoring

Now unused namespaces are grayed out suggesting you can eliminate them.

Refactoring

There are more options to fix the code and you can preview the changes inline.

preview

Removing temporary variables

Converts this:
  1. int val = 1 + 2;   
  2. Sample.DoSomething(val);  
To this:
  1. Sample.DoSomething(1 + 2);  
Inline rename

Can inline-rename variables by pressing F2.

Inline rename

Window Layouts

You can customize your windows and save it. To do that go to the Window menu and select Save Window Layout, give it a name and save it. You can have multiple Window Layouts that can be loaded at anytime. The shortcut keys are Ctrl + Alt + 1, 2, 3....

More

See more at Visual Studio 2015 Preview page.


Similar Articles