In this post we are going to see new features available in the C# 6.0 version, New version of C# have new features it's because of Roslyn compiler .
C# 6.0 features can be used in visual studio editor, by doing the following settings,
Features available are :
1. Auto-property initializes
2. Null value checking in object execution
3 Conditional Catch block
4. Expression bodied function
5. Expression bodied function members
6. Await in Catch/Finally block
7. Static members invocation
8. nameof function
9. Dictionary Initializers
10. String interpolation
Auto property initialize:
Here after we can initialize the value of property in the declaration itself, when declaring a property we can set the value of the property like below
Null value checking in object execution:
Now we can check the object null, before execute the in build method or properties of a object in a single, This can be done by write the ?. operator after the object. if the object is null then the further property or method will not executed and return have a false expression type
Conditional Catch Block:
From the new version we can place the condition on the Catch block, to tell the compiler which part of the Catch block need to be executed when the exception raised, based on the condition.
Expression bodied function:
Expression can be used in defining a function, Functions can be mention in a single line by the use of Expression statement
Expression bodied function members:
Expression can be used in defining a members of a class in a single line
Await in Catch/Finally block:
Now here after we can use the Await in Catch and Finally block in the code, this code is very useful for the async operations
Static members invocation:
In the newer version we can use the static methods straight away in the code , instead of using the Class as prefix, Here we are using the Class declaration in the namespace by using the Static keyword
nameof Function
nameof function is used for the developers to find the name of the object or property at the runtime, instead of declare the variable for the display name
Dictionary initialize
The new way of Dictionary Initialize will make a clear understanding dictionary among the developers
String interpolation
now we can format the string in a easy way by declare the variable directly in string by make a $interpolation declaration before string and declare the variable inside the curly braces
From this post you can learn some pf the new features available in the C# 6.0
C# 6.0 features can be used in visual studio editor, by doing the following settings,
- Go to the properties of a project
- select the Build tab
- click the Advanced button
- select the C# to 6.0
- Then click OK. Now the Visual studio using the C# 6.0 version to build the project.
Features available are :
1. Auto-property initializes
2. Null value checking in object execution
3 Conditional Catch block
4. Expression bodied function
5. Expression bodied function members
6. Await in Catch/Finally block
7. Static members invocation
8. nameof function
9. Dictionary Initializers
10. String interpolation
Auto property initialize:
Here after we can initialize the value of property in the declaration itself, when declaring a property we can set the value of the property like below
public string LastName { set; get; } = "G";
Null value checking in object execution:
Now we can check the object null, before execute the in build method or properties of a object in a single, This can be done by write the ?. operator after the object. if the object is null then the further property or method will not executed and return have a false expression type
Person employee = null;
employee?.GetFullName();
From the new version we can place the condition on the Catch block, to tell the compiler which part of the Catch block need to be executed when the exception raised, based on the condition.
try
{
employee = null;
throw new Exception("Sample error");
}
catch(Exception ex) when(employee==null)
{
WriteLine($"Employee is null {ex.Message}");
}
catch(Exception ex)
{
WriteLine($"Error Message is : {ex.Message}");
}
Expression bodied function:
Expression can be used in defining a function, Functions can be mention in a single line by the use of Expression statement
/* Expression body function */
public string GetFullName() => $"{FirstName} {LastName}";
Expression bodied function members:
Expression can be used in defining a members of a class in a single line
/* Expression body function members */
public string Address => "Chennai";
Now here after we can use the Await in Catch and Finally block in the code, this code is very useful for the async operations
catch(Exception ex)
{
await Logger.Write () ;
WriteLine($"Error Message is : {ex.Message}");
}
In the newer version we can use the static methods straight away in the code , instead of using the Class as prefix, Here we are using the Class declaration in the namespace by using the Static keyword
using static System.Console;
using static Samples.ExtensionMethods;
/* Name of a Object */
WriteLine("Hi");
nameof Function
nameof function is used for the developers to find the name of the object or property at the runtime, instead of declare the variable for the display name
/* Name of a Object */
WriteLine(nameof(employee));
Dictionary initialize
The new way of Dictionary Initialize will make a clear understanding dictionary among the developers
/* New Way of Dictionary intializers */
Dictionary<int, Person> emplist = new Dictionary<int, Person>()
{
[0] = new Person() { FirstName= "Suresh",LastName = "G",Age = 25},
[1] = new Person() { FirstName = "Ramu", LastName = "D" ,Age = 3},
[2] = new Person() { }
};
String interpolation
now we can format the string in a easy way by declare the variable directly in string by make a $interpolation declaration before string and declare the variable inside the curly braces
public static void PrintWithStyle(string messgae)
{
WriteLine("********************************");
WriteLine($"{messgae}");
WriteLine("********************************\n");
}
catch(Exception ex)
{
WriteLine($"Employee is null {ex.Message}");
}
From this post you can learn some pf the new features available in the C# 6.0