cs11dotnet7/vscode/Chapter03/SelectionStatements/Program.cs

107 lines
2.2 KiB
C#
Raw Normal View History

2022-02-18 13:13:25 +01:00
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);