Skip to content

Instantly share code, notes, and snippets.

@mirmostafa
Last active December 27, 2021 13:28
Show Gist options
  • Save mirmostafa/085afcfdcff8836dd715b0cef38c6960 to your computer and use it in GitHub Desktop.
Save mirmostafa/085afcfdcff8836dd715b0cef38c6960 to your computer and use it in GitHub Desktop.
Pattern Matchings
if (randomShape is Circle { Diameter: 10, Radius: < 5 and > 2, Area: <= 15 } c)
{
Console.WriteLine($"This is my circle. Area: {c.Area}");
}
if (randomShape is not null)
{
}
if (randomShape is not Recatngle)
{
}
if (randomShape is not null and not Recatngle)
{
}
if (randomShape is Circle { Name: { Length: >= 0 } })
{
}
if (randomShape is Circle { Name.Length: >= 0 })
{
}
switch (randomShape)
{
case Circle _:
Console.WriteLine();
break;
case Recatngle r when r.Width == r.Height:
Console.WriteLine("This is square.");
break;
default:
break;
}
var text = randomShape switch
{
Circle { Diameter: 20 } c1 => "This is a special circle",
Circle { Diameter: 10, Radius: < 5 and > 2, Area: <= 15 } c1 => "This is a very special circle",
Circle _ => "This is a circle",
Recatngle r when r.Width == r.Height => "This is a square",
Recatngle r => "This is a rectangle",
{ Area: 100 } => "This is a shape with area 100",
{ Area: > 100 } => "This is a shape with begger than 100",
_ => throw new NotImplementedException(),
};
var text1 = randomShape.Area switch
{
>= 100 and <= 200 => "Hi",
_ => "Bye"
};
switch (fruit)
{
case Apple apple when apple.Color == Color.Green:
var food = MakeApplePieFrom(apple);
break;
}
public static string Display(this Person person)
{
return (person?.Age < 50, IsYoung(person), IsBaby(person), person) switch
{
(_, _, _, Student p) => $"{p.Name} is a student and he studies {p.CourseOfStudy}.",// His birth year is {p.GetBirthYear()}",
(_, _, _, Teacher { Age: 43 } p) => $"{p.Name} is same as me and he teaches {p.CourseOfTeach}",
(_, true, _, Teacher p) => $"{p.Name} is a young teacher and he teaches {p.CourseOfTeach}",
(true, _, _, Teacher p) => $"{p.Name} is a teacher and he teaches {p.CourseOfTeach}",
(_, _, _, null) => "Null? kidding?",
(_, _, _, { Age: 0, Name: var name }) => $"I don't know how old is '{name}'",
(_, _, true, { } p) => $"{p.Name} is a baby. He is {p.Age}",
(_, _, _, { } p) => $"{p.Name} is a person and he {p.Age} is",
_ => $"What the f** is this?"
};
static bool IsYoung(Person p) => p?.Age > 40;
static bool IsBaby(Person p) => p?.Age < 10;
}
var isWeekEnd = DateTime.Now.DayOfWeek switch
{
DayOfWeek.Sunday or DayOfWeek.Monday => true,
_ => false
}
var isWeekEnd = DateTime.Now.DayOfWeek switch
{
DayOfWeek.Sunday or DayOfWeek.Monday => true,
_ => false
}
if ((a is { } b) && (b is Student c and { Age: > 5 }))
{
}
if (person is Student and { Name.Length: > 5 })
{
}
static bool isEnglish(char c) => (c is (>= 'A' and <= 'Z') or (>= 'a' and <= 'z'));
using System;
internal readonly record struct Person(string Name, int Age);
internal abstract class Shape
{
public abstract double Area { get; }
public string Name { get; set; }
}
internal sealed class Circle : Shape
{
public double Diameter { get; set; }
public override double Area => this.Radius * Math.PI;
public double Radius => this.Diameter / 2;
}
internal sealed class Recatngle : Shape
{
public double Width { get; set; }
public double Height { get; set; }
public override double Area => this.Width * this.Height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment