Last active
December 27, 2021 13:28
-
-
Save mirmostafa/085afcfdcff8836dd715b0cef38c6960 to your computer and use it in GitHub Desktop.
Pattern Matchings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
switch (randomShape) | |
{ | |
case Circle _: | |
Console.WriteLine(); | |
break; | |
case Recatngle r when r.Width == r.Height: | |
Console.WriteLine("This is square."); | |
break; | |
default: | |
break; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var text1 = randomShape.Area switch | |
{ | |
>= 100 and <= 200 => "Hi", | |
_ => "Bye" | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
switch (fruit) | |
{ | |
case Apple apple when apple.Color == Color.Green: | |
var food = MakeApplePieFrom(apple); | |
break; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isWeekEnd = DateTime.Now.DayOfWeek switch | |
{ | |
DayOfWeek.Sunday or DayOfWeek.Monday => true, | |
_ => false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isWeekEnd = DateTime.Now.DayOfWeek switch | |
{ | |
DayOfWeek.Sunday or DayOfWeek.Monday => true, | |
_ => false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ((a is { } b) && (b is Student c and { Age: > 5 })) | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (person is Student and { Name.Length: > 5 }) | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static bool isEnglish(char c) => (c is (>= 'A' and <= 'Z') or (>= 'a' and <= 'z')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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