This commit is contained in:
Oli Sturm
2022-02-17 16:07:37 +00:00
commit eecba258b3
13 changed files with 596 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+39
View File
@@ -0,0 +1,39 @@
namespace CS7 {
public class DeadPerson {
public string Name { get; init; } = "";
public virtual bool IsAlive => false;
}
public class Zombie : DeadPerson {
public override bool IsAlive => true;
}
class Program {
static void CheckZombie(DeadPerson p) {
switch (p) {
case Zombie z:
Console.WriteLine($"{z.Name} is a zombie, run!");
break;
default:
Console.WriteLine($"Looks like {p.Name} is safe.");
break;
}
}
static void Main(string[] args) {
string s = "This is a string";
if (s is string x)
Console.WriteLine($"Turns out s is the string '{x}'");
var d1 = new DeadPerson { Name = "Abe Lincoln" };
var d2 = new Zombie { Name = "Windows 7" };
CheckZombie(d1);
CheckZombie(d2);
Console.WriteLine("My heartfelt apologies in case anybody finds my zombie sample offensive.");
}
}
}