C Sharp: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 38: Line 38:
claas Test
claas Test
{
{
     public A {get; set;}
     public PropA {get; set;}
     public B {get; set;}
     public PropB {get; set;}
     public C {get; set;}
     public PropC {get; set;}
    public PropD {get; set;}


     Test(string a, string b, string c)
     Test(string a, string b, string c, string d)
     {
     {
       A = a;
       PropA = a;
       B = b;
       PropB = b;
       C = c;
       PropC = c;
      PropD = d;
     }
     }


Line 52: Line 54:
       out string a,
       out string a,
       out string b,
       out string b,
       out string c)
       out string c,
      out string d)
     {
     {
       a = A;
       a = PropA;
       b = B
       b = PropB
       c = C;
       c = PropC;
      d = PropD;
     }  
     }  
}
}

Revision as of 01:08, 10 August 2020

C# 8.0 and .Net Core 3.0

Intro

Here is the history of C#

  • 2.0 included generics
  • 3.0 Added Linq with Lambda and anonymous types
  • 4.0 Added Dynamics
  • 5.0 Added Async and Await
  • 6.0 Added Null propagating operator (targer?.IsActive ?? false;
  • 7.0 Expressions and Tuples

C# 8.0 brings

  • Nullable Reference Types
  • Pattern Matching
  • Indices and Ranges
  • Built-in Json Support
  • Windows Desktop Support
  • Build and Deploy Improvements
  • Other Language Improvements
  • Other .NET Core Platform Improvements

Nullable Reference Types

This allows you to specify a possibility of null. e.g.

class
{
    public string? Title {get; set;}
    public List<Comment> Title {get;} = new List<Comment>();
}

Bad code is highlighted a compile time with CS8618 Non-nullable property and via intellisence.

You and enable and restore this feature on a file basis using #nullable enable and #nullable restore

Pattern Matching

Deconstructors

Hardcoded unlike javascript

claas Test
{
    public PropA {get; set;}
    public PropB {get; set;}
    public PropC {get; set;}
    public PropD {get; set;}

    Test(string a, string b, string c, string d)
    {
      PropA = a;
      PropB = b;
      PropC = c;
      PropD = d;
    }

    void Deconstructor(
       out string a,
       out string b,
       out string c,
       out string d)
    {
       a = PropA;
       b = PropB
       c = PropC;
       d = PropD;
    } 
}

Positional Pattern

public static bool TestHasCValue5(test inTest)
{
    return s is Test(_,_,"5",_);
}

Property Pattern

public static bool IsUsBasedWithUkManager(object o)
{
    return o is Employee e && 
        e is { Region: "US", ReportsTo: {Region:" UK" }};
}

Tuple Pattern

public statis bool MakesBlue(Color c1, Color c2)
{
    return (c1,c2) is (Color.Red, Color.Green) ||
           (c2,c1) is (Color.Red, Color.Green);
}

Indices and Ranges

Built-in Json Support

Windows Desktop Support

Build and Deploy Improvements

Other Language Improvements

Other .NET Core Platform Improvements