Here is an overview of new features in C#:
- Out variables
- Pattern matching
- Tuples
- Deconstruction
- Discards
- Local Functions
- Binary Literals
- Digit Separators
- Ref returns and locals
- Generalized async return types
- More expression-bodied members
- Throw expressions
-
|
// Out variables |
|
|
|
// Old way |
|
int parsedOld; |
|
string parseMeOld = “222224“; |
|
|
|
int.TryParse(parseMeOld, out parsedOld); |
|
Console.WriteLine(parsedOld); |
|
|
|
// New way |
|
string parseMe = “3115“; |
|
|
|
int.TryParse(parseMe, out int parsed); |
|
Console.WriteLine(parsed); |
Tuples
| List<int> numbers = new List<int> { 3, 5, 11, 4, 7 }; |
|
var result = GetLowestHighest(numbers); |
|
|
|
Console.WriteLine(result.Item1); // ugly? |
|
Console.WriteLine(result.Item2); // yeah, ugly.. |
|
|
|
|
|
// Notice the return type of the method. It’s tuple |
|
private static(int, int) GetLowestHighest(IReadOnlyCollection < int > numbers) { |
|
int lowest = numbers.Min(n => n); |
|
int highest = numbers.Max(n => n); |
|
|
|
return (lowest, highest); |
|
} |