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:
- public class Sample
- {
- public Foo FooProperty { get; set; }
- }
-
- public class Foo
- {
- public Bar BarProperty { get; set; }
- }
-
- public class Bar
- {
- public void DoSomething()
- {
- Console.WriteLine("Doing something");
- }
- }
And you want to call
Bar.DoSomething you would need to check if the instances are not null until you get there:
- static void Main(string[] args)
- {
- Sample nullSample = null;
-
- if (nullSample != null
- && nullSample.FooProperty != null
- && nullSample.FooProperty.BarProperty != null)
- {
- nullSample.FooProperty.BarProperty.DoSomething();
- }
- }
With this new null conditional operator you can simplify to this:
- static void Main(string[] args)
- {
- Sample nullSample = null;
-
- nullSample?.FooProperty?.BarProperty?.DoSomething();
- }
Auto-Property InitializersSometimes 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.
- public class Sample
- {
- public Sample()
- {
- Name = "Default initial value";
- }
- public string Name { get; set; }
- }
With the Auto-Property Initializer your can do that without having to set it on the constructor.
- public class Sample
- {
- public string Name { get; set; } = "Default initial value";
- }
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.
- public class Sample(string name)
- {
- public string Name { get; set; } = name;
- }
nameof ExpressionsThe
nameof expression lets you grab the name of the variable you are working with. It can be used in the
NotifyPropertyChanged method, for example:
- public class Sample : INotifyPropertyChanged
- {
- private string name;
-
- public string Name
- {
- get { return name; }
- set
- {
- name = value;
- NotifyPropertyChanged(nameof(Name));
- }
- }
-
- public void NotifyPropertyChanged(string propName)
- {
- if (PropertyChanged != null)
- PropertyChanged(this,
- new PropertyChangedEventArgs(propName));
- }
-
- public event PropertyChangedEventHandler PropertyChanged;
- }
It could also be used on an exception:
- public void DoSomething(object someParam)
- {
- if (someParam == null)
- {
- throw new ArgumentNullException(nameof(someParam));
- }
-
- }
So rather than hard-coding magic strings you can use this expression.
String interpolation
You often must write:
- 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.
- String.Format($"hello {sample.Name}");
Expression-bodied Members
You can define method bodies using lambdas.
- public class Sample
- {
- public string Name { get; } = "Foo";
-
- public override string ToString() => Name;
- }
Exception filters
You can choose when to catch an exception, for example:
- public void DoSomething()
- {
- bool exceptionCatchingEnabled = true;
-
- try
- {
- }
- catch (Exception) when (exceptionCatchingEnabled)
- {
- }
- }
Visual Studio 2015
Improved ToolTips
Now tooltips include colors, glyph and more information.
Code Fixes and Refactoring
Now unused namespaces are grayed out suggesting you can eliminate them.
There are more options to fix the code and you can preview the changes inline.
Removing temporary variables
Converts this:
- int val = 1 + 2;
- Sample.DoSomething(val);
To this:
- Sample.DoSomething(1 + 2);
Inline rename
Can inline-rename variables by pressing F2.
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.