Monday, December 9, 2019

C# 8.0 new features :



1) Default Interface Methods

interface IWriteLine 

 public void WriteLine() 
 { 
 Console.WriteLine("Wow C# 8!"); 
 } 


2) Nullable reference type

string? nullableString = null; 

Console.WriteLine(nullableString.Length);

3) Advanced Pattern Matching

Example
var point = new 3DPoint(1, 2, 3); //x=1, y=2, z=3 
if (point is 3DPoint(1, var myY, _)) 

  // Code here will be executed only if the point .X == 1, myY is a new variable 
  // that can be used in this scope. 

}

4) Async streams

await foreach (var x in enumerable) 

  Console.WriteLine(x); 

}

5) Ranges

Index i1 = 3; // number 3 from beginning 
Index i2 = ^4; // number 4 from end 
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 

Console.WriteLine($"{a[i1]}, {a[i2]}"); // "3, 6" 

6) Caller Expression Attribute

public static class Verify { 
    public static void InRange(int argument, int low, int high, 
        [CallerArgumentExpression("argument")] string argumentExpression = null, 
        [CallerArgumentExpression("low")] string lowExpression = null, 
        [CallerArgumentExpression("high")] string highExpression = null) { 
        if (argument < low) { 
            throw new ArgumentOutOfRangeException(paramName: argumentExpression, message: $ " {argumentExpression} ({argument}) cannot be less than {lowExpression} ({low})."); 
        } 
        if (argument > high) { 
            throw new ArgumentOutOfRangeException(paramName: argumentExpression, message: $ "{argumentExpression} ({argument}) cannot be greater than {highExpression} ({high})."); 
        } 
    } 
    public static void NotNull < T > (T argument, 
        [CallerArgumentExpression("argument")] string argumentExpression = null) 
    where T: class { 
        if (argument == null) throw new ArgumentNullException(paramName: argumentExpression); 
    } 

// CallerArgumentExpression: convert the expressions to a string!   
Verify.NotNull(array); // paramName: "array"   
// paramName: "index"   
// Error message by wrong Index:   
"index (-1) cannot be less than 0 (0).", or 
// "index (6) cannot be greater than array.Length - 1 (5)."   

Verify.InRange(index, 0, array.Length - 1);

7) Default in deconstruction

(int x, string y) = (default, default); // C# 7 

(int x, string y) = default;               // C# 8

8) Using declarations

// C# Oldy Style 
using (var repository = new Repository())   
{   
} // repository is disposed here!   
   
// vs.C# 8   
   
using var repository = new Repository();   
Console.WriteLine(repository.First());   

// repository is disposed here! 

9) Generic attributes

public class GenericAttribute<T> : Attribute { } 
public class ValidatesAttribute<T> : Attribute {} 
[Validates<string>] 
public class StringValidation {} 
[Validates<int>] 

public class IntegerValidation{} 

10) Static Local Functions

int AddFiveAndSeven() 

  int y = 5; int x = 7; 
  return Add(x, y); 
  static int Add(int left, int right) => left + right; 

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...