See what’s new in C# 6 and Visual Studio 2015

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

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

From MSDN Blog, IDE features.

C# 6.0

Null Conditional Operator

I bet you have loads of null checks in your code before attempting to call 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 wanted to call Bar.DoSomething you would have 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 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.

public class Sample
{
    public Sample()
    {
        Name = "Default initial value";
    }
    public string Name { get; set; }
}

With the Auto-Property Initializer your can do the same 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 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:

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 have to write:

String.Format("hello {0}", sample.Name);

And the code might require new parameters so you have 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 which allows us to add parameter values directly in the string.

To do that you have to 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.

2015-51-07 04-51-27-708

2015-52-07 04-52-24-884

Code Fixes and Refactoring

Now unused namespaces are grayed out suggesting you can get rid of them.

2015-53-07 04-53-06-414

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

2015-54-07 04-54-30-512

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.

2015-03-07 05-03-35-101

Window Layouts

You can customize your windows and save them. To do that go to the Window menu > Save Window Layout, give it a name and save. 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s