Skip to content

Instantly share code, notes, and snippets.

@ManojLingala
Created May 8, 2024 11:11
Show Gist options
  • Save ManojLingala/3d302937c58460b0d49d83c8c9ad37cb to your computer and use it in GitHub Desktop.
Save ManojLingala/3d302937c58460b0d49d83c8c9ad37cb to your computer and use it in GitHub Desktop.
ExcitingFeatures
1. public record Person(string FirstName, string LastName);
2.var person1 = new Person("Manoj", "L");
var person2 = person1 with { LastName = "Lingala" };
// Prints: Person { FirstName = Manoj, LastName = Lingala }
Console.WriteLine(person2);
3.public class Car
{
public string Model { get; init; }
public string Color { get; init; }
}
var car = new Car { Model = "Tesla", Color = "Red" };
// car.Model = "BMW"; // Compile-time error
4.
// Before C# 10
namespace MyNamespace
{
public class MyClass { }
}
// C# 10
namespace MyNamespace;
public class MyClass { }
5.
// File: GlobalUsings.cs
global using System;
global using System.Collections.Generic;
///Pattern Matching
6. bool IsLetter(char c) => c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';
7. string ClassifyTemperature(int temp) => temp switch
{
< 0 => "Freezing",
<= 15 => "Cold",
<= 30 => "Warm",
_ => "Hot"
};
8.string GetSeason(Month month) => month switch
{
Month.December or Month.January or Month.February => "Winter",
Month.March or Month.April or Month.May => "Spring",
Month.June or Month.July or Month.August => "Summer",
Month.September or Month.October or Month.November => "Fall",
_ => throw new ArgumentOutOfRangeException(nameof(month))
};
9.bool IsFirstElementZero(int[] numbers) => numbers is [0, ..];
bool HasAtLeastThreeElements(int[] numbers) => numbers is [_, _, _, ..];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment