Skip to content

Instantly share code, notes, and snippets.

@dannyskoog
Last active February 11, 2022 15:44
Show Gist options
  • Save dannyskoog/c9a61620a6f00430d8b728ef481164cc to your computer and use it in GitHub Desktop.
Save dannyskoog/c9a61620a6f00430d8b728ef481164cc to your computer and use it in GitHub Desktop.
C# 9 - What's new?
// C# 8
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
}
// C# 9
System.Console.WriteLine("Hello World!");
var person1 = new Person("Danny", "Skoog"); // OK
var person2 = new Person("Danny", "Skoog") { FirstName = "Mike" }; // OK
person1.FirstName = "Mike"; // NOT OK
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
public Person(string firstName, string lastName) => (FirstName, LastName) = (firstName, lastName);
}
// Declaration
public record Person(string FirstName, string LastName);
// Equality
var person1 = new Person("Danny", "Skoog");
var person2 = new Person("Danny", "Skoog");
Console.WriteLine(person1 == person2); // Outputs "True"
// Immutability
var person3 = new Person("Danny", "Skoog"); // OK
var person4 = new Person("Danny", "Skoog") { FirstName = "Mike", LastName = "Johnson" }; // OK
var person5 = person1 with { FirstName = "Mike" }; // OK
person3.FirstName = "Mike"; // NOT OK
// ToString
Console.WriteLine(person1.ToString()); // Outputs "Person { FirstName = Danny, LastName = Skoog }"
// Deconstruction
var (firstName, lastName) = person1;
// Type patterns - Example 1
// C# 8
static int GetSpeed(Vehicle vehicle) => vehicle switch
{
Bus _ => 75,
Car _ => 100,
_ => 50
};
// C# 9
static int GetSpeed2(Vehicle vehicle) => vehicle switch
{
Bus => 75,
Car => 100,
_ => 50
};
// Type patterns - Example 2
// C# 8
static bool IsDannyTuple((object, object) tuple)
{
return tuple is (string _, int _);
}
// C# 9
static bool IsDannyTuple2((object, object) tuple)
{
return tuple is (string, int);
}
// Relational patterns - Example 1
// C# 8
static int GetTax(int muncipalityId) => muncipalityId switch
{
0 => 20,
int n when n < 5 => 21,
int n when n <= 10 => 22,
int n when n > 21 => 23,
_ => 20
};
// C# 9
static int GetTax2(int muncipalityId) => muncipalityId switch
{
0 => 20,
< 5 => 21,
<= 10 => 22,
> 21 => 23,
_ => 20
};
// Relational patterns - Example 2
// C# 8
bool IsLetter(char c) => c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
// C# 9
bool IsLetter2(char c) => c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';
// Logical patterns - Example 1
// C# 8
static int GetTax3(int muncipalityId) => muncipalityId switch
{
int n when n == 0 || n == 1 => 20,
int n when n > 1 && n < 5 => 21,
int n when n > 5 && n != 7 => 22,
7 => 23,
_ => 20
};
// C# 9
static int GetTax4(int muncipalityId) => muncipalityId switch
{
0 or 1 => 20,
> 1 and < 5 => 21,
> 5 and not 7 => 22,
7 => 23,
_ => 20
};
// Logical patterns - Example 2
// C# 8
static int GetSpeed3(Vehicle vehicle)
{
if (!(vehicle is null))
{
if (!(vehicle is Bus))
return 100;
return 70;
}
return 0;
}
// C# 9
static int GetSpeed4(Vehicle vehicle)
{
if (vehicle is not null)
{
if (vehicle is not Bus)
return 100;
return 70;
}
return 0;
}
// Logical patterns - Example 3
// C# 8
static int GetSpeed5(Vehicle vehicle) => !(vehicle is Bus) ? 100 : 70;
// C# 9
static int GetSpeed6(Vehicle vehicle) => vehicle is not Bus ? 100 : 70;
// Parenthesized patterns
// C# 9
bool IsLetter3(char c) => c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z');
// Example 1
// C# 8
Person person = new Person();
// C# 9
Person person2 = new();
// Example 2
// C# 8
DoIt(new Person());
// C# 9
DoIt(new());
// Example 3
// C# 8
Person GetPerson()
{
return new Person();
}
// C# 9
Person GetPerson2()
{
return new();
}
// C# 8
Console.CancelKeyPress += (_, b) => Console.WriteLine("Cancel key was pressed");
// C# 9
Console.CancelKeyPress += (_, _) => Console.WriteLine("Cancel key was pressed");
var x = 5;
// C# 8
Func<int, int> func = (int num) => x + 5; // OK
// C# 9
Func<int, int> func2 = static (int num) => x + 5; // NOT OK
int Calculate(int? num)
{
int CalculateLocal([DisallowNull] int? num)
{
return num.GetValueOrDefault() + 10;
}
return CalculateLocal(num);
}
Calculate(100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment