08-01-2016 11:54:11
C# / C# BASICS
0 Bookmark(s)
849 View(s)
Its C# 6.0. Yes new version of C# which is coming with Visual Studio 2015 has few amazing features. Many of those new features are given below.
Null-Conditional Operator
C# 6.0 includes a new null-conditional operator that helps you write code which handles NullReferenceException more efficiently.
int length = text?.Length;
Also it includes below features.
- Return null if the operand is null
- Short-circuit additional invocations in the call chain if the operand is null
- Return a nullable type (System.Nullable) if the target member returns a value type
- Support delegate invocation in a thread safe manner
Auto-Property Initializers
public class FingerPrint
{
public DateTime TimeStamp { get; } = DateTime.UtcNow;
public string User { get; } =
System.Security.Principal.WindowsPrincipal.Current.Identity.Name;
public string Process { get; } =
System.Diagnostics.Process.GetCurrentProcess().ProcessName;
}
Nameof Expressions
void ThrowArgumentNullExceptionUsingNameOf(string param1)
{
throw new ArgumentNullException(nameof(param1));
}
Primary Constructors
If you specify additional constructors, the constructor call chain must invoke the primary constructor last. This means a primary constructor can’t have a this initializer. All other constructors must have them, assuming the primary constructor isn’t also the default constructor:
public class Patent(string title, string yearOfPublication)
{
public Patent(string title, string yearOfPublication,
IEnumerable inventors)
...this(title, yearOfPublication)
{
Inventors.AddRange(inventors);
}
}