mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-05 06:15:24 +00:00
Initial commit
This commit is contained in:
parent
ca7684985d
commit
49bdd4dda1
60 changed files with 1527 additions and 30 deletions
106
vscode/Chapter03/SelectionStatements/Program.cs
Normal file
106
vscode/Chapter03/SelectionStatements/Program.cs
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
string password = "ninja";
|
||||
if (password.Length < 8)
|
||||
{
|
||||
WriteLine("Your password is too short. Use at least 8 characters.");
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteLine("Your password is strong.");
|
||||
}
|
||||
|
||||
// add and remove the "" to change the behavior
|
||||
object o = 3;
|
||||
int j = 4;
|
||||
|
||||
if (o is int i)
|
||||
{
|
||||
WriteLine($"{i} x {j} = {i * j}");
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteLine("o is not an int so it cannot multiply!");
|
||||
}
|
||||
|
||||
int number = Random.Shared.Next(1, 7);
|
||||
WriteLine($"My random number is {number}");
|
||||
|
||||
switch (number)
|
||||
{
|
||||
case 1:
|
||||
WriteLine("One");
|
||||
break; // jumps to end of switch statement
|
||||
case 2:
|
||||
WriteLine("Two");
|
||||
goto case 1;
|
||||
case 3: // multiple case section
|
||||
case 4:
|
||||
WriteLine("Three or four");
|
||||
goto case 1;
|
||||
case 5:
|
||||
goto A_label;
|
||||
default:
|
||||
WriteLine("Default");
|
||||
break;
|
||||
} // end of switch statement
|
||||
WriteLine("After end of switch");
|
||||
A_label:
|
||||
WriteLine($"After A_label");
|
||||
|
||||
// string path = "/Users/markjprice/cs11dotnet7/Chapter03";
|
||||
string path = @"C:\cs11dotnet7\Chapter03";
|
||||
Stream? s;
|
||||
|
||||
Write("Press R for read-only or W for writeable: ");
|
||||
ConsoleKeyInfo key = ReadKey();
|
||||
|
||||
if (key.Key == ConsoleKey.R)
|
||||
{
|
||||
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;
|
||||
switch (s)
|
||||
{
|
||||
case FileStream writeableFile when s.CanWrite:
|
||||
message = "The stream is a file that I can write to.";
|
||||
break;
|
||||
case FileStream readOnlyFile:
|
||||
message = "The stream is a read-only file.";
|
||||
break;
|
||||
case MemoryStream ms:
|
||||
message = "The stream is a memory address.";
|
||||
break;
|
||||
default: // always evaluated last despite its current position
|
||||
message = "The stream is some other type.";
|
||||
break;
|
||||
case null:
|
||||
message = "The stream is null.";
|
||||
break;
|
||||
}
|
||||
WriteLine(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.",
|
||||
null
|
||||
=> "The stream is null.",
|
||||
_
|
||||
=> "The stream is some other type."
|
||||
};
|
||||
WriteLine(message);
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue