Features of C# 9.0 – Part 1
Jan 23, 2021 02:33 0 Comments C# (C-Sharp) PARTH

Features of C# 9.0 – Part 1

 

Microsoft release C# version 9.0 in November 2020 with some exciting features. Let’s see some of them in this blog –

 

  • Records

 

A record is a new C# data type that is immutable by default. The equality between Records is compared by structure or by reference equality.

 

C#9 records: immutable classes - NDepend

 

Example –

 

Publicrecord Employee(stringName, intAge);

public recordEmployee(stringName, intAge)

{public string Name { get; set; } = Name;

public int Age{ get; set; } = Age;

}

var employee1 = newEmployee("Parth Jolly", 29);

var employee2= newEmployee("Siddharth Jolly", 25);

 

Console.WriteLine(employee1 == employee2);

Console.WriteLine(employee1.Equals(employee2));

Console.WriteLine(ReferenceEquals(employee1, employee2));

// Changing the value

varemployee3 = employee1 with{ Age = 35};

Console.WriteLine(employee1 == employee3);

var(name, age) = employee3;

employee1.Age = 35;

publicrecord EmployeeInherit(string Name, int Age, string Country): Employee(Name, Age);

varemployeeInherited = new EmployeeInherit(“Parth Jolly”, 29 , “India”);

Console.WriteLine(employee1 == employeeInherited);

  • Function Pointers

This feature allows the developer to use delegate* for the declaration of function pointers.

 

 

Example –

unsafe class FunctionPtr

{

static int GetLength(string str) => str.Length;

delegate* fnPtr=&GetLength;

PublicvoidDemo()

{

Console.WriteLine(fnPtr("Parth"));

}

}

 

Output – 4

  • Pattern Matching

There are some enhancements to Pattern Matching which are introduced in C# 9. These are –

 

Using Pattern Matching to Avoid Massive “if” Statements | by Changhui Xu |  Level Up Coding

 

  • Type patterns: They are used to match the input against a type. If the input type is a match to the type specified in the pattern, the match succeeds.

 

Example –

 

Objectcheck= new int();

var getType= checkswitch

{

string=> "string",

int=> "int"

};

Console.WriteLine(getType);

Output –int

And patterns: They are used to  represent the logical “and” of the two sub-patterns.

 

Example –

 

varemployee = newEmployee("Parth", 29);

varage= employeeswitch

{

Employee(_, <18) => "less than 18",

("Parth",_) and (_,>18)=>"Parth is greater than 18"

};

Console.WriteLine(age);

 

Output -Parth is greater than 18

 

 

  • Or patterns: They are used torepresent the logical “or” of the two sub-patterns.

 

Example –

 

var employee = new Employee("Parth", 29);

varage= employee switch

{

Employee(_, <18) => "less than 18",

("Parth",_) or (_,>18)=>"Parth is greater than or equal to 18"

};

Console.WriteLine(age);

 

Output - Parth is greater than or equal to 18

 

 

  • Negated not pattern: They are the patterns that requires not to matcha given pattern. 

 

Example –

 

var employee = new Employee("Parth", 29);

varwho = employeeswitch

{

not("Parth", 29) => "who’s this”,

_=>"It’s me"

};

Console.WriteLine(who);

 

Output –It’s me

 

 

  • Parenthesized patterns: They provide a flexibility to the programmer to put parentheses around any pattern.

 

Example –

 

public record IsNumberFive(boolvalid, intNumber);

varisNumberFive = newIsNumberFive(true, 5);

varn = isNumberFive switch

{

((_,>1and <5) and (_,>5and <9)) or (_,5) => "It is 5",

_=> "It is not 5"

};

Console.WriteLine(n);

 

Output –It is 5

 

 

  • Relational patterns: They provide a flexibility to the programmer to match their input against constant values to determine if the input is greater than, less than or equal to that constant.

 

Example –

 

var employee1 = new Employee("Parth", 29);

var employee2 = new Employee("Siddharth", 18);

var age = employee1 switch

Employee(_, < 18) => "age is less than 18",

(_, > 18) => "age is greater than 18",

(_, 18) => "age is 18 years old"

};

Console.WriteLine(age);

 

Output -age is greater than 18

 

 

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect