C# 7 is the latest version of C# language and its available in Visual Studio 2017.
Let us check one by one what's new in C#.
1. Out Variables
You can declare out variable as a parameter to the method where they are used.
In the previous version, you need to separate the declare out variable and initialize into two statement.
2. Tuples
Tuples are lightweight data structures that contain multiple fields to represent the data members.
Tuples support more than one data element structure.
You can create a tuple by assigning each member to a value:
3. Pattern matching
Pattern matching is a feature that allows you to implement method dispatch on properties other than the type of an object.
Let's see is expression and switch expression
- is expression
- switch statement updates
Switch statement already part old c# language.
For other features and more detail visit the Microsoft websites.
Let us check one by one what's new in C#.
1. Out Variables
You can declare out variable as a parameter to the method where they are used.
In the previous version, you need to separate the declare out variable and initialize into two statement.
int count; if(int.TryParse(input, out count)) { Response.Write(count) }Now, You can write out variable as an argument so no need to write a separate statement.
if(int.TryParse(input, out int count)) { Response.Write(count) }
2. Tuples
Tuples are lightweight data structures that contain multiple fields to represent the data members.
Tuples support more than one data element structure.
You can create a tuple by assigning each member to a value:
var letters = ("a", "b");Tuples are most useful as return types for private and internal methods. Tuples provide a simple syntax for those methods to return multiple discrete values: You save the work of authoring a class or a struct that defines the type returned. There is no need for creating a new type.
private static (int a, int b) Range(int a, int b) { return (a, b); }Call above method and set return values to the variables.
(int a, int b) = Range(a,b)
3. Pattern matching
Pattern matching is a feature that allows you to implement method dispatch on properties other than the type of an object.
Let's see is expression and switch expression
- is expression
public static int sumData(IEnumerable<object> values) { var sum = 0; foreach(var item in values) { if (item is int val) sum += val; } return sum; }Above example, item check is int val then sum.
- switch statement updates
Switch statement already part old c# language.
public static int SumData(IEnumerable<object> values) { var sum = 0; foreach (var item in values) { switch (item) { case int val: sum += val; break; } } return sum; }The match expressions have a slightly different syntax than the is expressions, where you declare the type and variable at the beginning of the case expression.
For other features and more detail visit the Microsoft websites.
0 comments:
Post a Comment