mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
Initial commit
This commit is contained in:
parent
7a4066f965
commit
bf63cf7d4b
|
|
@ -68,3 +68,39 @@ for (int row = 0; row <= jagged.GetUpperBound(0); row++)
|
|||
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.",
|
||||
};
|
||||
|
|
|
|||
16
vs4win/Chapter03/SelectionStatements/Animals.cs
Normal file
16
vs4win/Chapter03/SelectionStatements/Animals.cs
Normal 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;
|
||||
}
|
||||
|
|
@ -46,61 +46,60 @@ WriteLine("After end of switch");
|
|||
A_label:
|
||||
WriteLine($"After A_label");
|
||||
|
||||
// string path = "/Users/markjprice/cs11dotnet7/Chapter03";
|
||||
string path = @"C:\cs11dotnet7\Chapter03";
|
||||
Stream? s;
|
||||
// Pattern matching with the switch statement
|
||||
|
||||
Write("Press R for read-only or W for writeable: ");
|
||||
ConsoleKeyInfo key = ReadKey();
|
||||
Animal?[] animals = new Animal?[]
|
||||
{
|
||||
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(
|
||||
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;
|
||||
|
||||
string message;
|
||||
switch (s)
|
||||
{
|
||||
case FileStream writeableFile when s.CanWrite:
|
||||
message = "The stream is a file that I can write to.";
|
||||
switch (animal)
|
||||
{
|
||||
case Cat fourLeggedCat when fourLeggedCat.Legs == 4:
|
||||
message = $"The cat named {fourLeggedCat.Name} has four legs.";
|
||||
break;
|
||||
case FileStream readOnlyFile:
|
||||
message = "The stream is a read-only file.";
|
||||
case Cat wildCat when wildCat.IsDomestic == false:
|
||||
message = $"The non-domestic cat is named {wildCat.Name}.";
|
||||
break;
|
||||
case MemoryStream ms:
|
||||
message = "The stream is a memory address.";
|
||||
case Cat cat:
|
||||
message = $"The cat is named {cat.Name}.";
|
||||
break;
|
||||
default: // always evaluated last despite its current position
|
||||
message = "The stream is some other type.";
|
||||
default: // default is always evaluated last
|
||||
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;
|
||||
case null:
|
||||
message = "The stream is null.";
|
||||
message = "The animal is null.";
|
||||
break;
|
||||
}
|
||||
WriteLine(message);
|
||||
}
|
||||
WriteLine($"switch statement: {message}");
|
||||
|
||||
message = s switch
|
||||
{
|
||||
FileStream writeableFile when s.CanWrite
|
||||
=> "The stream is a file that I can write to.",
|
||||
FileStream readOnlyFile
|
||||
=> "The stream is a read-only file.",
|
||||
MemoryStream ms
|
||||
=> "The stream is a memory address.",
|
||||
message = animal switch
|
||||
{
|
||||
Cat fourLeggedCat when fourLeggedCat.Legs == 4
|
||||
=> $"The cat named {fourLeggedCat.Name} has four legs.",
|
||||
Cat wildCat when wildCat.IsDomestic == false
|
||||
=> $"The non-domestic cat is named {wildCat.Name}.",
|
||||
Cat cat
|
||||
=> $"The cat is named {cat.Name}.",
|
||||
Spider spider when spider.IsPoisonous
|
||||
=> $"The {spider.Name} spider is poisonous. Run!",
|
||||
null
|
||||
=> "The stream is null.",
|
||||
=> "The animal is null.",
|
||||
_
|
||||
=> "The stream is some other type."
|
||||
};
|
||||
WriteLine(message);
|
||||
=> $"The animal named {animal.Name} is a {animal.GetType().Name}."
|
||||
};
|
||||
WriteLine($"switch expression: {message}");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,3 +68,39 @@ for (int row = 0; row <= jagged.GetUpperBound(0); row++)
|
|||
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.",
|
||||
};
|
||||
|
|
|
|||
16
vscode/Chapter03/SelectionStatements/Animals.cs
Normal file
16
vscode/Chapter03/SelectionStatements/Animals.cs
Normal 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;
|
||||
}
|
||||
|
|
@ -46,61 +46,60 @@ WriteLine("After end of switch");
|
|||
A_label:
|
||||
WriteLine($"After A_label");
|
||||
|
||||
// string path = "/Users/markjprice/cs11dotnet7/Chapter03";
|
||||
string path = @"C:\cs11dotnet7\Chapter03";
|
||||
Stream? s;
|
||||
// Pattern matching with the switch statement
|
||||
|
||||
Write("Press R for read-only or W for writeable: ");
|
||||
ConsoleKeyInfo key = ReadKey();
|
||||
Animal?[] animals = new Animal?[]
|
||||
{
|
||||
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(
|
||||
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;
|
||||
|
||||
string message;
|
||||
switch (s)
|
||||
{
|
||||
case FileStream writeableFile when s.CanWrite:
|
||||
message = "The stream is a file that I can write to.";
|
||||
switch (animal)
|
||||
{
|
||||
case Cat fourLeggedCat when fourLeggedCat.Legs == 4:
|
||||
message = $"The cat named {fourLeggedCat.Name} has four legs.";
|
||||
break;
|
||||
case FileStream readOnlyFile:
|
||||
message = "The stream is a read-only file.";
|
||||
case Cat wildCat when wildCat.IsDomestic == false:
|
||||
message = $"The non-domestic cat is named {wildCat.Name}.";
|
||||
break;
|
||||
case MemoryStream ms:
|
||||
message = "The stream is a memory address.";
|
||||
case Cat cat:
|
||||
message = $"The cat is named {cat.Name}.";
|
||||
break;
|
||||
default: // always evaluated last despite its current position
|
||||
message = "The stream is some other type.";
|
||||
default: // default is always evaluated last
|
||||
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;
|
||||
case null:
|
||||
message = "The stream is null.";
|
||||
message = "The animal is null.";
|
||||
break;
|
||||
}
|
||||
WriteLine(message);
|
||||
}
|
||||
WriteLine($"switch statement: {message}");
|
||||
|
||||
message = s switch
|
||||
{
|
||||
FileStream writeableFile when s.CanWrite
|
||||
=> "The stream is a file that I can write to.",
|
||||
FileStream readOnlyFile
|
||||
=> "The stream is a read-only file.",
|
||||
MemoryStream ms
|
||||
=> "The stream is a memory address.",
|
||||
message = animal switch
|
||||
{
|
||||
Cat fourLeggedCat when fourLeggedCat.Legs == 4
|
||||
=> $"The cat named {fourLeggedCat.Name} has four legs.",
|
||||
Cat wildCat when wildCat.IsDomestic == false
|
||||
=> $"The non-domestic cat is named {wildCat.Name}.",
|
||||
Cat cat
|
||||
=> $"The cat is named {cat.Name}.",
|
||||
Spider spider when spider.IsPoisonous
|
||||
=> $"The {spider.Name} spider is poisonous. Run!",
|
||||
null
|
||||
=> "The stream is null.",
|
||||
=> "The animal is null.",
|
||||
_
|
||||
=> "The stream is some other type."
|
||||
};
|
||||
WriteLine(message);
|
||||
=> $"The animal named {animal.Name} is a {animal.GetType().Name}."
|
||||
};
|
||||
WriteLine($"switch expression: {message}");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue