C# v6.0 - Visual Studio 2015

improvements to windows forms controls resizing
edit and continue for 64 bit apps
async-aware debugging
windows store app development


Null-Conditional Operator


return myvalue?.SubString(0,5); 

Instead of

string smaller = myvalue; 
if (myvalue != null)
{
   smaller = myvalue.SubString(0,5);
}
return smaller


PropertyChanged?.Invoke(propertyChanged(this, new PropertyChangedEventArgs(nameof(Name)); 


Auto-Property Initializers

This allows assignment of properties directly within their declaration
Before you could only initialise the value of a property in the classes constructor.


class MyClass 
{
   public string Property_One { get; } = System.Environment.Username;
}

Instead of

class MyClass 
{
   public string Property_One()
   {
      Property_One - System.Environment.Username;
   }
   public string Property_One { get; }
}


Exception Filters

Lets you specify a condition on a catch block.

try 
{
   throw new Exception("error");
}
catch (Exception ex) if (ex.Message = "text")
{
}
catch (Exception ex) if (ex.Message = "error")
{
   // this executes
}



async

Lets you log exceptions without blocking the current thread


nameof Expressions

This provides access to element names whether they are class names, method names, parameter names or attribute names
There is a new operator called nameof that lets you remove any string literals that represent the names of any members



Primary Constructors

This allows you to use the class declaration to specify the most common constructor

class Person(string name, int age) 
{
   private string _name = name;
   private int _age = age;
}

instead of

class Person(string name, int age) 
{
   public Person (string name, int age)
   {
      _name = name;
      _age = age;
   }
}


Expression Bodied Functions



Static Using Namespace

You often find yourself repeating the class name as a prefix for the static members.

using static System.Console; 
using static System.Math;
WriteLine("some text");
Sqrt(4);

instead of

using System.Console; 
using System.Math;
Console.WriteLine("some text")
Math.Sqrt(4);


String Interpolation

There is a cleaner format to allow you put expressions directly in the string literal to evaluate expressions.

string onestring = "one"; 
string twostring = "two";
WriteLine("contents : \ {onestring} \ {twostring}");



Dictionary Initializers

There is now another way which is a more readable format

public Dictionary  MyDictionary = new Dictionary() 
{
   {"Russell", "London"}
   {"Steven","New York"}
}

instead of

public Dictionary  MyDictionary = new Dictionary() 
{
   ["Russell"] = "London"
   ["Steven"] = "New York"
}





© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

PrevNext