Skip to content

Instantly share code, notes, and snippets.

@aelij

aelij/cs7-match.csx

Last active Apr 20, 2016
Embed
What would you like to do?
C# 7 Experiments for RoslynPad
abstract class Animal { }
class Dog : Animal
{
public string Bark() => "Woof";
}
class Cat : Animal
{
public string Meow() => "Meow";
}
class Giraffe : Animal { }
var animals = new Animal[] { new Dog(), new Cat(), new Giraffe() };
var animalsWithSounds =
from animal in animals
let sound = animal match
(
case Dog d : d.Bark()
case Cat c : c.Meow()
case * : "-"
)
select new { Type = animal, Sound = sound };
animalsWithSounds.Dump();
object[] numebrs = { 0b1, 0b10, new object[] { 0b100, 0b1000, 0b1_0000 }, 0b10_0000 };
int Tally(IEnumerable<object> list)
{
int s = 0;
foreach (var v in list)
{
switch (v)
{
case int i:
s += i;
break;
case IEnumerable<object> l when l.Any():
s += Tally(l);
break;
}
}
return s;
}
Tally(numebrs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.