Initial commit

This commit is contained in:
Mark J Price 2022-09-17 14:09:41 +01:00
parent 7a4066f965
commit bf63cf7d4b
6 changed files with 212 additions and 110 deletions

View file

@ -68,3 +68,39 @@ for (int row = 0; row <= jagged.GetUpperBound(0); row++)
WriteLine($"Row {row}, Column {col}: {jagged[row][col]}"); WriteLine($"Row {row}, Column {col}: {jagged[row][col]}");
} }
} }
// Pattern matching with arrays
int[] sequentialNumbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] oneTwoNumbers = new int[] { 1, 2 };
int[] oneTwoTenNumbers = new int[] { 1, 2, 10 };
int[] oneTwoThreeTenNumbers = new int[] { 1, 2, 3, 10 };
int[] primeNumbers = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
int[] fibonacciNumbers = new int[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
int[] emptyNumbers = new int[] { };
int[] threeNumbers = new int[] { 9, 7, 5 };
int[] sixNumbers = new int[] { 9, 7, 5, 4, 2, 10 };
WriteLine($"{nameof(sequentialNumbers)}: {CheckSwitch(sequentialNumbers)}");
WriteLine($"{nameof(oneTwoNumbers)}: {CheckSwitch(oneTwoNumbers)}");
WriteLine($"{nameof(oneTwoTenNumbers)}: {CheckSwitch(oneTwoTenNumbers)}");
WriteLine($"{nameof(oneTwoThreeTenNumbers)}: {CheckSwitch(oneTwoThreeTenNumbers)}");
WriteLine($"{nameof(primeNumbers)}: {CheckSwitch(primeNumbers)}");
WriteLine($"{nameof(fibonacciNumbers)}: {CheckSwitch(fibonacciNumbers)}");
WriteLine($"{nameof(emptyNumbers)}: {CheckSwitch(emptyNumbers)}");
WriteLine($"{nameof(threeNumbers)}: {CheckSwitch(threeNumbers)}");
WriteLine($"{nameof(sixNumbers)}: {CheckSwitch(sixNumbers)}");
static string CheckSwitch(int[] values) => values switch
{
[] => "Empty array",
[1, 2, _, 10] => "Contains 1, 2, any single number, 10.",
[1, 2, .., 10] => "Contains 1, 2, any range including empty, 10.",
[1, 2] => "Contains 1 then 2.",
[int item1, int item2, int item3] =>
$"Contains {item1} then {item2} then {item3}.",
[0, _] => "Starts with 0, then one other number.",
[0, ..] => "Starts with 0, then any range of numbers.",
[2, .. int[] others] => $"Starts with 2, then {others.Length} more numbers.",
[..] => "Any items in any order.",
};

View file

@ -0,0 +1,16 @@
class Animal // This is the base type for all animals.
{
public string? Name;
public DateTime Born;
public byte Legs;
}
class Cat : Animal // This is a subtype of animal.
{
public bool IsDomestic;
}
class Spider : Animal // This is another subtype of animal.
{
public bool IsPoisonous;
}

View file

@ -46,61 +46,60 @@ WriteLine("After end of switch");
A_label: A_label:
WriteLine($"After A_label"); WriteLine($"After A_label");
// string path = "/Users/markjprice/cs11dotnet7/Chapter03"; // Pattern matching with the switch statement
string path = @"C:\cs11dotnet7\Chapter03";
Stream? s;
Write("Press R for read-only or W for writeable: "); Animal?[] animals = new Animal?[]
ConsoleKeyInfo key = ReadKey(); {
new Cat { Name = "Karen", Born = new(year: 2022, month: 8, day: 23),
Legs = 4, IsDomestic = true },
null,
new Cat { Name = "Mufasa", Born = new(year: 1994, month: 6, day: 12) },
new Spider { Name = "Sid Vicious", Born = DateTime.Today,
IsPoisonous = true},
new Spider { Name = "Captain Furry", Born = DateTime.Today }
};
if (key.Key == ConsoleKey.R) foreach (Animal? animal in animals)
{ {
s = File.Open( string message;
Path.Combine(path, "file.txt"),
FileMode.OpenOrCreate,
FileAccess.Read);
}
else
{
s = File.Open(
Path.Combine(path, "file.txt"),
FileMode.OpenOrCreate,
FileAccess.Write);
}
WriteLine();
string message; switch (animal)
switch (s) {
{ case Cat fourLeggedCat when fourLeggedCat.Legs == 4:
case FileStream writeableFile when s.CanWrite: message = $"The cat named {fourLeggedCat.Name} has four legs.";
message = "The stream is a file that I can write to.";
break; break;
case FileStream readOnlyFile: case Cat wildCat when wildCat.IsDomestic == false:
message = "The stream is a read-only file."; message = $"The non-domestic cat is named {wildCat.Name}.";
break; break;
case MemoryStream ms: case Cat cat:
message = "The stream is a memory address."; message = $"The cat is named {cat.Name}.";
break; break;
default: // always evaluated last despite its current position default: // default is always evaluated last
message = "The stream is some other type."; message = $"The animal named {animal.Name} is a {animal.GetType().Name}.";
break;
case Spider spider when spider.IsPoisonous:
message = $"The {spider.Name} spider is poisonous. Run!";
break; break;
case null: case null:
message = "The stream is null."; message = "The animal is null.";
break; break;
} }
WriteLine(message); WriteLine($"switch statement: {message}");
message = s switch message = animal switch
{ {
FileStream writeableFile when s.CanWrite Cat fourLeggedCat when fourLeggedCat.Legs == 4
=> "The stream is a file that I can write to.", => $"The cat named {fourLeggedCat.Name} has four legs.",
FileStream readOnlyFile Cat wildCat when wildCat.IsDomestic == false
=> "The stream is a read-only file.", => $"The non-domestic cat is named {wildCat.Name}.",
MemoryStream ms Cat cat
=> "The stream is a memory address.", => $"The cat is named {cat.Name}.",
Spider spider when spider.IsPoisonous
=> $"The {spider.Name} spider is poisonous. Run!",
null null
=> "The stream is null.", => "The animal is null.",
_ _
=> "The stream is some other type." => $"The animal named {animal.Name} is a {animal.GetType().Name}."
}; };
WriteLine(message); WriteLine($"switch expression: {message}");
}

View file

@ -68,3 +68,39 @@ for (int row = 0; row <= jagged.GetUpperBound(0); row++)
WriteLine($"Row {row}, Column {col}: {jagged[row][col]}"); WriteLine($"Row {row}, Column {col}: {jagged[row][col]}");
} }
} }
// Pattern matching with arrays
int[] sequentialNumbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] oneTwoNumbers = new int[] { 1, 2 };
int[] oneTwoTenNumbers = new int[] { 1, 2, 10 };
int[] oneTwoThreeTenNumbers = new int[] { 1, 2, 3, 10 };
int[] primeNumbers = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
int[] fibonacciNumbers = new int[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
int[] emptyNumbers = new int[] { };
int[] threeNumbers = new int[] { 9, 7, 5 };
int[] sixNumbers = new int[] { 9, 7, 5, 4, 2, 10 };
WriteLine($"{nameof(sequentialNumbers)}: {CheckSwitch(sequentialNumbers)}");
WriteLine($"{nameof(oneTwoNumbers)}: {CheckSwitch(oneTwoNumbers)}");
WriteLine($"{nameof(oneTwoTenNumbers)}: {CheckSwitch(oneTwoTenNumbers)}");
WriteLine($"{nameof(oneTwoThreeTenNumbers)}: {CheckSwitch(oneTwoThreeTenNumbers)}");
WriteLine($"{nameof(primeNumbers)}: {CheckSwitch(primeNumbers)}");
WriteLine($"{nameof(fibonacciNumbers)}: {CheckSwitch(fibonacciNumbers)}");
WriteLine($"{nameof(emptyNumbers)}: {CheckSwitch(emptyNumbers)}");
WriteLine($"{nameof(threeNumbers)}: {CheckSwitch(threeNumbers)}");
WriteLine($"{nameof(sixNumbers)}: {CheckSwitch(sixNumbers)}");
static string CheckSwitch(int[] values) => values switch
{
[] => "Empty array",
[1, 2, _, 10] => "Contains 1, 2, any single number, 10.",
[1, 2, .., 10] => "Contains 1, 2, any range including empty, 10.",
[1, 2] => "Contains 1 then 2.",
[int item1, int item2, int item3] =>
$"Contains {item1} then {item2} then {item3}.",
[0, _] => "Starts with 0, then one other number.",
[0, ..] => "Starts with 0, then any range of numbers.",
[2, .. int[] others] => $"Starts with 2, then {others.Length} more numbers.",
[..] => "Any items in any order.",
};

View file

@ -0,0 +1,16 @@
class Animal // This is the base type for all animals.
{
public string? Name;
public DateTime Born;
public byte Legs;
}
class Cat : Animal // This is a subtype of animal.
{
public bool IsDomestic;
}
class Spider : Animal // This is another subtype of animal.
{
public bool IsPoisonous;
}

View file

@ -46,61 +46,60 @@ WriteLine("After end of switch");
A_label: A_label:
WriteLine($"After A_label"); WriteLine($"After A_label");
// string path = "/Users/markjprice/cs11dotnet7/Chapter03"; // Pattern matching with the switch statement
string path = @"C:\cs11dotnet7\Chapter03";
Stream? s;
Write("Press R for read-only or W for writeable: "); Animal?[] animals = new Animal?[]
ConsoleKeyInfo key = ReadKey(); {
new Cat { Name = "Karen", Born = new(year: 2022, month: 8, day: 23),
Legs = 4, IsDomestic = true },
null,
new Cat { Name = "Mufasa", Born = new(year: 1994, month: 6, day: 12) },
new Spider { Name = "Sid Vicious", Born = DateTime.Today,
IsPoisonous = true},
new Spider { Name = "Captain Furry", Born = DateTime.Today }
};
if (key.Key == ConsoleKey.R) foreach (Animal? animal in animals)
{ {
s = File.Open( string message;
Path.Combine(path, "file.txt"),
FileMode.OpenOrCreate,
FileAccess.Read);
}
else
{
s = File.Open(
Path.Combine(path, "file.txt"),
FileMode.OpenOrCreate,
FileAccess.Write);
}
WriteLine();
string message; switch (animal)
switch (s) {
{ case Cat fourLeggedCat when fourLeggedCat.Legs == 4:
case FileStream writeableFile when s.CanWrite: message = $"The cat named {fourLeggedCat.Name} has four legs.";
message = "The stream is a file that I can write to.";
break; break;
case FileStream readOnlyFile: case Cat wildCat when wildCat.IsDomestic == false:
message = "The stream is a read-only file."; message = $"The non-domestic cat is named {wildCat.Name}.";
break; break;
case MemoryStream ms: case Cat cat:
message = "The stream is a memory address."; message = $"The cat is named {cat.Name}.";
break; break;
default: // always evaluated last despite its current position default: // default is always evaluated last
message = "The stream is some other type."; message = $"The animal named {animal.Name} is a {animal.GetType().Name}.";
break;
case Spider spider when spider.IsPoisonous:
message = $"The {spider.Name} spider is poisonous. Run!";
break; break;
case null: case null:
message = "The stream is null."; message = "The animal is null.";
break; break;
} }
WriteLine(message); WriteLine($"switch statement: {message}");
message = s switch message = animal switch
{ {
FileStream writeableFile when s.CanWrite Cat fourLeggedCat when fourLeggedCat.Legs == 4
=> "The stream is a file that I can write to.", => $"The cat named {fourLeggedCat.Name} has four legs.",
FileStream readOnlyFile Cat wildCat when wildCat.IsDomestic == false
=> "The stream is a read-only file.", => $"The non-domestic cat is named {wildCat.Name}.",
MemoryStream ms Cat cat
=> "The stream is a memory address.", => $"The cat is named {cat.Name}.",
Spider spider when spider.IsPoisonous
=> $"The {spider.Name} spider is poisonous. Run!",
null null
=> "The stream is null.", => "The animal is null.",
_ _
=> "The stream is some other type." => $"The animal named {animal.Name} is a {animal.GetType().Name}."
}; };
WriteLine(message); WriteLine($"switch expression: {message}");
}