Initial commit

This commit is contained in:
Mark J Price 2022-02-18 12:13:25 +00:00
parent ca7684985d
commit 49bdd4dda1
60 changed files with 1527 additions and 30 deletions

View file

@ -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>

View file

@ -0,0 +1,15 @@
WriteLine("--------------------------------------------------------------------------");
WriteLine("Type Byte(s) of memory Min Max");
WriteLine("--------------------------------------------------------------------------");
WriteLine($"sbyte {sizeof(sbyte),-4} {sbyte.MinValue,30} {sbyte.MaxValue,30}");
WriteLine($"byte {sizeof(byte),-4} {byte.MinValue,30} {byte.MaxValue,30}");
WriteLine($"short {sizeof(short),-4} {short.MinValue,30} {short.MaxValue,30}");
WriteLine($"ushort {sizeof(ushort),-4} {ushort.MinValue,30} {ushort.MaxValue,30}");
WriteLine($"int {sizeof(int),-4} {int.MinValue,30} {int.MaxValue,30}");
WriteLine($"uint {sizeof(uint),-4} {uint.MinValue,30} {uint.MaxValue,30}");
WriteLine($"long {sizeof(long),-4} {long.MinValue,30} {long.MaxValue,30}");
WriteLine($"ulong {sizeof(ulong),-4} {ulong.MinValue,30} {ulong.MaxValue,30}");
WriteLine($"float {sizeof(float),-4} {float.MinValue,30} {float.MaxValue,30}");
WriteLine($"double {sizeof(double),-4} {double.MinValue,30} {double.MaxValue,30}");
WriteLine($"decimal {sizeof(decimal),-4} {decimal.MinValue,30} {decimal.MaxValue,30}");
WriteLine("--------------------------------------------------------------------------");

View file

@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Arguments", "Arguments\Argu
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncConsole", "AsyncConsole\AsyncConsole.csproj", "{459F1756-610C-4C65-9335-C269DE60AFD3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch02Ex03Numbers", "Ch02Ex03Numbers\Ch02Ex03Numbers.csproj", "{0E82C0D9-93DA-406D-985D-1111B1C70989}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -45,6 +47,10 @@ Global
{459F1756-610C-4C65-9335-C269DE60AFD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{459F1756-610C-4C65-9335-C269DE60AFD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{459F1756-610C-4C65-9335-C269DE60AFD3}.Release|Any CPU.Build.0 = Release|Any CPU
{0E82C0D9-93DA-406D-985D-1111B1C70989}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E82C0D9-93DA-406D-985D-1111B1C70989}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E82C0D9-93DA-406D-985D-1111B1C70989}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E82C0D9-93DA-406D-985D-1111B1C70989}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View file

@ -1,10 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Using Include="System.Console" Static="true" />
</ItemGroup>
</Project>

View file

@ -15,7 +15,7 @@ string[] names2 = new[] { "Kate", "Jack", "Rebecca", "Tom" };
for (int i = 0; i < names2.Length; i++)
{
// output the item at index position i
Console.WriteLine(names2[i]);
WriteLine(names2[i]);
}
string[,] grid1 = new[,] // two dimensions
@ -25,16 +25,16 @@ string[,] grid1 = new[,] // two dimensions
{ "Aardvark", "Bear", "Cat", "Dog" }
};
Console.WriteLine($"Lower bound of the first dimension is: {grid1.GetLowerBound(0)}");
Console.WriteLine($"Upper bound of the first dimension is: {grid1.GetUpperBound(0)}");
Console.WriteLine($"Lower bound of the second dimension is: {grid1.GetLowerBound(1)}");
Console.WriteLine($"Upper bound of the second dimension is: {grid1.GetUpperBound(1)}");
WriteLine($"Lower bound of the first dimension is: {grid1.GetLowerBound(0)}");
WriteLine($"Upper bound of the first dimension is: {grid1.GetUpperBound(0)}");
WriteLine($"Lower bound of the second dimension is: {grid1.GetLowerBound(1)}");
WriteLine($"Upper bound of the second dimension is: {grid1.GetUpperBound(1)}");
for (int row = 0; row <= grid1.GetUpperBound(0); row++)
{
for (int col = 0; col <= grid1.GetUpperBound(1); col++)
{
Console.WriteLine($"Row {row}, Column {col}: {grid1[row, col]}");
WriteLine($"Row {row}, Column {col}: {grid1[row, col]}");
}
}
@ -51,12 +51,12 @@ string[][] jagged = new[] // array of string arrays
new[] { "Aardvark", "Bear" }
};
Console.WriteLine("Upper bound of array of arrays is: {0}",
WriteLine("Upper bound of array of arrays is: {0}",
jagged.GetUpperBound(0));
for (int array = 0; array <= jagged.GetUpperBound(0); array++)
{
Console.WriteLine("Upper bound of array {0} is: {1}",
WriteLine("Upper bound of array {0} is: {1}",
arg0: array,
arg1: jagged[array].GetUpperBound(0));
}
@ -65,6 +65,6 @@ for (int row = 0; row <= jagged.GetUpperBound(0); row++)
{
for (int col = 0; col <= jagged[row].GetUpperBound(0); col++)
{
Console.WriteLine($"Row {row}, Column {col}: {jagged[row][col]}");
WriteLine($"Row {row}, Column {col}: {jagged[row][col]}");
}
}

View file

@ -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>

View file

@ -0,0 +1,30 @@
int a = 10; // 00001010
int b = 6; // 00000110
WriteLine($"a = {a}");
WriteLine($"b = {b}");
WriteLine($"a & b = {a & b}"); // 2-bit column only
WriteLine($"a | b = {a | b}"); // 8, 4, and 2-bit columns
WriteLine($"a ^ b = {a ^ b}"); // 8 and 4-bit columns
// 01010000 left-shift a by three bit columns
WriteLine($"a << 3 = {a << 3}");
// multiply a by 8
WriteLine($"a * 8 = {a * 8}");
// 00000011 right-shift b by one bit column
WriteLine($"b >> 1 = {b >> 1}");
WriteLine();
WriteLine("Outputting integers as binary:");
WriteLine($"a = {ToBinaryString(a)}");
WriteLine($"b = {ToBinaryString(b)}");
WriteLine($"a & b = {ToBinaryString(a & b)}");
WriteLine($"a | b = {ToBinaryString(a | b)}");
WriteLine($"a ^ b = {ToBinaryString(a ^ b)}");
static string ToBinaryString(int value)
{
return Convert.ToString(value, toBase: 2).PadLeft(8, '0');
}

View file

@ -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>

View file

@ -0,0 +1,27 @@
bool a = true;
bool b = false;
WriteLine($"AND | a | b ");
WriteLine($"a | {a & a,-5} | {a & b,-5} ");
WriteLine($"b | {b & a,-5} | {b & b,-5} ");
WriteLine();
WriteLine($"OR | a | b ");
WriteLine($"a | {a | a,-5} | {a | b,-5} ");
WriteLine($"b | {b | a,-5} | {b | b,-5} ");
WriteLine();
WriteLine($"XOR | a | b ");
WriteLine($"a | {a ^ a,-5} | {a ^ b,-5} ");
WriteLine($"b | {b ^ a,-5} | {b ^ b,-5} ");
WriteLine();
WriteLine($"a & DoStuff() = {a & DoStuff()}");
WriteLine($"b & DoStuff() = {b & DoStuff()}");
WriteLine();
WriteLine($"a && DoStuff() = {a && DoStuff()}");
WriteLine($"b && DoStuff() = {b && DoStuff()}");
static bool DoStuff()
{
WriteLine("I am doing some stuff.");
return true;
}

View file

@ -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>

View file

@ -0,0 +1,92 @@
using static System.Convert;
int a = 10;
double b = a; // an int can be safely cast into a double
WriteLine(b);
double c = 9.8;
int d = (int)c;
WriteLine(d); // d is 9 losing the .8 part
long e = 5_000_000_000;
int f = (int)e;
WriteLine($"e is {e:N0} and f is {f:N0}");
e = long.MaxValue;
f = (int)e;
WriteLine($"e is {e:N0} and f is {f:N0}");
double g = 9.8;
int h = ToInt32(g); // a method of System.Convert
WriteLine($"g is {g} and h is {h}");
// Understanding the default rounding rules
double[] doubles = new[]
{ 9.49, 9.5, 9.51, 10.49, 10.5, 10.51 };
foreach (double n in doubles)
{
WriteLine($"ToInt32({n}) is {ToInt32(n)}");
}
// Taking control of rounding rules
foreach (double n in doubles)
{
WriteLine(format:
"Math.Round({0}, 0, MidpointRounding.AwayFromZero) is {1}",
arg0: n,
arg1: Math.Round(value: n, digits: 0,
mode: MidpointRounding.AwayFromZero));
}
// Converting from any type to a string
int number = 12;
WriteLine(number.ToString());
bool boolean = true;
WriteLine(boolean.ToString());
DateTime now = DateTime.Now;
WriteLine(now.ToString());
object me = new();
WriteLine(me.ToString());
// allocate array of 128 bytes
byte[] binaryObject = new byte[128];
// populate array with random bytes
Random.Shared.NextBytes(binaryObject);
WriteLine("Binary Object as bytes:");
for (int index = 0; index < binaryObject.Length; index++)
{
Write($"{binaryObject[index]:X} ");
}
WriteLine();
// convert to Base64 string and output as text
string encoded = ToBase64String(binaryObject);
WriteLine($"Binary Object as Base64: {encoded}");
int age = int.Parse("27");
DateTime birthday = DateTime.Parse("4 July 1980");
WriteLine($"I was born {age} years ago.");
WriteLine($"My birthday is {birthday}.");
WriteLine($"My birthday is {birthday:D}.");
Write("How many eggs are there? ");
string? input = ReadLine(); // or use "12" in notebook
if (int.TryParse(input, out int count))
{
WriteLine($"There are {count} eggs.");
}
else
{
WriteLine("I could not parse the input.");
}

View file

@ -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>

View file

@ -0,0 +1,15 @@
checked
{
try
{
int max = 500;
for (byte i = 0; i < max; i++)
{
WriteLine(i);
}
}
catch (OverflowException ex)
{
WriteLine($"{ex.GetType()} says {ex.Message}");
}
}

View file

@ -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>

View file

@ -0,0 +1,26 @@
for (int i = 1; i <= 100; i++)
{
if (i % 15 == 0)
{
Write("FizzBuzz");
}
else if (i % 5 == 0)
{
Write("Buzz");
}
else if (i % 3 == 0)
{
Write("Fizz");
}
else
{
Write(i);
}
// put a comma and space after every number except 100
if (i < 100) Write(", ");
// write a carriage-return after every ten numbers
if (i % 10 == 0) WriteLine();
}
WriteLine();

View file

@ -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>

View file

@ -0,0 +1,19 @@
Write("Enter a number between 0 and 255: ");
string n1 = ReadLine()!;
Write("Enter another number between 0 and 255: ");
string n2 = ReadLine()!;
try
{
byte a = byte.Parse(n1);
byte b = byte.Parse(n2);
int answer = a / b;
WriteLine($"{a} divided by {b} is {answer}");
}
catch (Exception ex)
{
WriteLine($"{ex.GetType().Name}: {ex.Message}");
}

View file

@ -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>

View file

@ -0,0 +1,23 @@
WriteLine("int x = 3;");
WriteLine("int y = 2 + ++x;");
int x = 3;
int y = 2 + ++x;
WriteLine($"x = {x} and y = {y}");
WriteLine();
WriteLine("x = 3 << 2;");
WriteLine("y = 10 >> 1;");
x = 3 << 2;
y = 10 >> 1; ;
WriteLine($"x = {x} and y = {y}");
WriteLine();
WriteLine("x = 10 & 8;");
WriteLine("y = 10 | 7;");
x = 10 & 8;
y = 10 | 7;
WriteLine($"x = {x} and y = {y}");
WriteLine();

View file

@ -0,0 +1,97 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32126.317
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Operators", "Operators\Operators.csproj", "{900C1311-F1B3-4AA5-A56E-31343288B1E6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BooleanOperators", "BooleanOperators\BooleanOperators.csproj", "{AC2A75A6-439A-40ED-994A-C5948288D8D0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitwiseAndShiftOperators", "BitwiseAndShiftOperators\BitwiseAndShiftOperators.csproj", "{8EDD188D-E374-494E-A4FE-447EDDAB7A79}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SelectionStatements", "SelectionStatements\SelectionStatements.csproj", "{32763A06-A929-4FC1-883D-9928800FF25B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IterationStatements", "IterationStatements\IterationStatements.csproj", "{B2DEEF3D-14EB-4AC0-A2B8-7ED027F070C3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Arrays", "Arrays\Arrays.csproj", "{CE49D514-6C03-4A56-9AB0-65283E60F380}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CastingConverting", "CastingConverting\CastingConverting.csproj", "{F51A3966-F274-4919-A76E-2FA2D94174AD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HandlingExceptions", "HandlingExceptions\HandlingExceptions.csproj", "{CF84BF74-5BFC-489C-A0CF-B0DC9FDCC277}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CheckingForOverflow", "CheckingForOverflow\CheckingForOverflow.csproj", "{134F43C1-DD0B-4E8F-8C93-20264991EB8B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch03Ex02LoopsAndOverflow", "Ch03Ex02LoopsAndOverflow\Ch03Ex02LoopsAndOverflow.csproj", "{EB069D8A-49CF-4451-8F71-21D8F486B2C5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch03Ex03FizzBuzz", "Ch03Ex03FizzBuzz\Ch03Ex03FizzBuzz.csproj", "{090C0933-15B2-4A66-9215-7A9EAB0D4B8F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch03Ex04Exceptions", "Ch03Ex04Exceptions\Ch03Ex04Exceptions.csproj", "{DAE099EB-2F72-4B8F-BAC9-A98868F0EB17}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch03Ex05Operators", "Ch03Ex05Operators\Ch03Ex05Operators.csproj", "{C52FC40B-5C3F-4E3E-9228-2B23DEDFD7B1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{900C1311-F1B3-4AA5-A56E-31343288B1E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{900C1311-F1B3-4AA5-A56E-31343288B1E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{900C1311-F1B3-4AA5-A56E-31343288B1E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{900C1311-F1B3-4AA5-A56E-31343288B1E6}.Release|Any CPU.Build.0 = Release|Any CPU
{AC2A75A6-439A-40ED-994A-C5948288D8D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC2A75A6-439A-40ED-994A-C5948288D8D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC2A75A6-439A-40ED-994A-C5948288D8D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC2A75A6-439A-40ED-994A-C5948288D8D0}.Release|Any CPU.Build.0 = Release|Any CPU
{8EDD188D-E374-494E-A4FE-447EDDAB7A79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8EDD188D-E374-494E-A4FE-447EDDAB7A79}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EDD188D-E374-494E-A4FE-447EDDAB7A79}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EDD188D-E374-494E-A4FE-447EDDAB7A79}.Release|Any CPU.Build.0 = Release|Any CPU
{32763A06-A929-4FC1-883D-9928800FF25B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{32763A06-A929-4FC1-883D-9928800FF25B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{32763A06-A929-4FC1-883D-9928800FF25B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{32763A06-A929-4FC1-883D-9928800FF25B}.Release|Any CPU.Build.0 = Release|Any CPU
{B2DEEF3D-14EB-4AC0-A2B8-7ED027F070C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B2DEEF3D-14EB-4AC0-A2B8-7ED027F070C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B2DEEF3D-14EB-4AC0-A2B8-7ED027F070C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B2DEEF3D-14EB-4AC0-A2B8-7ED027F070C3}.Release|Any CPU.Build.0 = Release|Any CPU
{CE49D514-6C03-4A56-9AB0-65283E60F380}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE49D514-6C03-4A56-9AB0-65283E60F380}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE49D514-6C03-4A56-9AB0-65283E60F380}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE49D514-6C03-4A56-9AB0-65283E60F380}.Release|Any CPU.Build.0 = Release|Any CPU
{F51A3966-F274-4919-A76E-2FA2D94174AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F51A3966-F274-4919-A76E-2FA2D94174AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F51A3966-F274-4919-A76E-2FA2D94174AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F51A3966-F274-4919-A76E-2FA2D94174AD}.Release|Any CPU.Build.0 = Release|Any CPU
{CF84BF74-5BFC-489C-A0CF-B0DC9FDCC277}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF84BF74-5BFC-489C-A0CF-B0DC9FDCC277}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF84BF74-5BFC-489C-A0CF-B0DC9FDCC277}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF84BF74-5BFC-489C-A0CF-B0DC9FDCC277}.Release|Any CPU.Build.0 = Release|Any CPU
{134F43C1-DD0B-4E8F-8C93-20264991EB8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{134F43C1-DD0B-4E8F-8C93-20264991EB8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{134F43C1-DD0B-4E8F-8C93-20264991EB8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{134F43C1-DD0B-4E8F-8C93-20264991EB8B}.Release|Any CPU.Build.0 = Release|Any CPU
{EB069D8A-49CF-4451-8F71-21D8F486B2C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB069D8A-49CF-4451-8F71-21D8F486B2C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB069D8A-49CF-4451-8F71-21D8F486B2C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB069D8A-49CF-4451-8F71-21D8F486B2C5}.Release|Any CPU.Build.0 = Release|Any CPU
{090C0933-15B2-4A66-9215-7A9EAB0D4B8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{090C0933-15B2-4A66-9215-7A9EAB0D4B8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{090C0933-15B2-4A66-9215-7A9EAB0D4B8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{090C0933-15B2-4A66-9215-7A9EAB0D4B8F}.Release|Any CPU.Build.0 = Release|Any CPU
{DAE099EB-2F72-4B8F-BAC9-A98868F0EB17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DAE099EB-2F72-4B8F-BAC9-A98868F0EB17}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DAE099EB-2F72-4B8F-BAC9-A98868F0EB17}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DAE099EB-2F72-4B8F-BAC9-A98868F0EB17}.Release|Any CPU.Build.0 = Release|Any CPU
{C52FC40B-5C3F-4E3E-9228-2B23DEDFD7B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C52FC40B-5C3F-4E3E-9228-2B23DEDFD7B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C52FC40B-5C3F-4E3E-9228-2B23DEDFD7B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C52FC40B-5C3F-4E3E-9228-2B23DEDFD7B1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6E9CB3A2-3A2D-46E1-8644-0A62A02BAC03}
EndGlobalSection
EndGlobal

View file

@ -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>

View file

@ -0,0 +1,28 @@
try
{
checked
{
int x = int.MaxValue - 1;
WriteLine($"Initial value: {x}");
x++;
WriteLine($"After incrementing: {x}");
x++;
WriteLine($"After incrementing: {x}");
x++;
WriteLine($"After incrementing: {x}");
}
}
catch (OverflowException)
{
WriteLine("The code overflowed but I caught the exception.");
}
unchecked
{
int y = int.MaxValue + 1;
WriteLine($"Initial value: {y}");
y--;
WriteLine($"After decrementing: {y}");
y--;
WriteLine($"After decrementing: {y}");
}

View file

@ -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>

View file

@ -0,0 +1,40 @@
WriteLine("Before parsing");
Write("What is your age? ");
string? input = ReadLine(); // or use "49" in a notebook
try
{
int age = int.Parse(input!);
WriteLine($"You are {age} years old.");
}
catch (OverflowException)
{
WriteLine("Your age is a valid number format but it is either too big or small.");
}
catch (FormatException)
{
WriteLine("The age you entered is not a valid number format.");
}
catch (Exception ex)
{
WriteLine($"{ex.GetType()} says {ex.Message}");
}
WriteLine("After parsing");
Write("Enter an amount: ");
string amount = ReadLine()!;
if (string.IsNullOrEmpty(amount)) return;
try
{
decimal amountValue = decimal.Parse(amount);
WriteLine($"Amount formatted as currency: {amountValue:C}");
}
catch (FormatException) when (amount.Contains("$"))
{
WriteLine("Amounts cannot use the dollar sign!");
}
catch (FormatException)
{
WriteLine("Amounts must only contain digits!");
}

View file

@ -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>

View file

@ -0,0 +1,47 @@
// Looping with the while statement
int x = 0;
while (x < 10)
{
WriteLine(x);
x++;
}
// Looping with the do statement
string? password;
int attempts = 0;
do
{
attempts++;
Write("Enter your password: ");
password = ReadLine();
}
while ((password != "Pa$$w0rd") & (attempts < 10));
if (attempts < 10)
{
WriteLine("Correct!");
}
else
{
WriteLine("You have used 10 attempts!");
}
// Looping with the for statement
for (int y = 1; y <= 10; y++)
{
WriteLine(y);
}
// Looping with the foreach statement
string[] names = { "Adam", "Barry", "Charlie" };
foreach (string name in names)
{
WriteLine($"{name} has {name.Length} characters.");
}

View file

@ -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>

View file

@ -0,0 +1,20 @@
int a = 3;
int b = a++;
WriteLine($"a is {a}, b is {b}");
int c = 3;
int d = ++c; // increment c before assigning it
WriteLine($"c is {c}, d is {d}");
int e = 11;
int f = 3;
WriteLine($"e is {e}, f is {f}");
WriteLine($"e + f = {e + f}");
WriteLine($"e - f = {e - f}");
WriteLine($"e * f = {e * f}");
WriteLine($"e / f = {e / f}");
WriteLine($"e % f = {e % f}");
double g = 11.0;
WriteLine($"g is {g:N1}, f is {f}");
WriteLine($"g / f = {g / f}");

View 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);

View file

@ -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>

View file

@ -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>

View file

@ -0,0 +1,15 @@
WriteLine("--------------------------------------------------------------------------");
WriteLine("Type Byte(s) of memory Min Max");
WriteLine("--------------------------------------------------------------------------");
WriteLine($"sbyte {sizeof(sbyte),-4} {sbyte.MinValue,30} {sbyte.MaxValue,30}");
WriteLine($"byte {sizeof(byte),-4} {byte.MinValue,30} {byte.MaxValue,30}");
WriteLine($"short {sizeof(short),-4} {short.MinValue,30} {short.MaxValue,30}");
WriteLine($"ushort {sizeof(ushort),-4} {ushort.MinValue,30} {ushort.MaxValue,30}");
WriteLine($"int {sizeof(int),-4} {int.MinValue,30} {int.MaxValue,30}");
WriteLine($"uint {sizeof(uint),-4} {uint.MinValue,30} {uint.MaxValue,30}");
WriteLine($"long {sizeof(long),-4} {long.MinValue,30} {long.MaxValue,30}");
WriteLine($"ulong {sizeof(ulong),-4} {ulong.MinValue,30} {ulong.MaxValue,30}");
WriteLine($"float {sizeof(float),-4} {float.MinValue,30} {float.MaxValue,30}");
WriteLine($"double {sizeof(double),-4} {double.MinValue,30} {double.MaxValue,30}");
WriteLine($"decimal {sizeof(decimal),-4} {decimal.MinValue,30} {decimal.MaxValue,30}");
WriteLine("--------------------------------------------------------------------------");

View file

@ -17,6 +17,9 @@
},
{
"path": "Vocabulary"
},
{
"path": "Ch02Ex03Numbers"
}
]
}

View file

@ -1,10 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Using Include="System.Console" Static="true" />
</ItemGroup>
</Project>

View file

@ -15,7 +15,7 @@ string[] names2 = new[] { "Kate", "Jack", "Rebecca", "Tom" };
for (int i = 0; i < names2.Length; i++)
{
// output the item at index position i
Console.WriteLine(names2[i]);
WriteLine(names2[i]);
}
string[,] grid1 = new[,] // two dimensions
@ -25,16 +25,16 @@ string[,] grid1 = new[,] // two dimensions
{ "Aardvark", "Bear", "Cat", "Dog" }
};
Console.WriteLine($"Lower bound of the first dimension is: {grid1.GetLowerBound(0)}");
Console.WriteLine($"Upper bound of the first dimension is: {grid1.GetUpperBound(0)}");
Console.WriteLine($"Lower bound of the second dimension is: {grid1.GetLowerBound(1)}");
Console.WriteLine($"Upper bound of the second dimension is: {grid1.GetUpperBound(1)}");
WriteLine($"Lower bound of the first dimension is: {grid1.GetLowerBound(0)}");
WriteLine($"Upper bound of the first dimension is: {grid1.GetUpperBound(0)}");
WriteLine($"Lower bound of the second dimension is: {grid1.GetLowerBound(1)}");
WriteLine($"Upper bound of the second dimension is: {grid1.GetUpperBound(1)}");
for (int row = 0; row <= grid1.GetUpperBound(0); row++)
{
for (int col = 0; col <= grid1.GetUpperBound(1); col++)
{
Console.WriteLine($"Row {row}, Column {col}: {grid1[row, col]}");
WriteLine($"Row {row}, Column {col}: {grid1[row, col]}");
}
}
@ -51,12 +51,12 @@ string[][] jagged = new[] // array of string arrays
new[] { "Aardvark", "Bear" }
};
Console.WriteLine("Upper bound of array of arrays is: {0}",
WriteLine("Upper bound of array of arrays is: {0}",
jagged.GetUpperBound(0));
for (int array = 0; array <= jagged.GetUpperBound(0); array++)
{
Console.WriteLine("Upper bound of array {0} is: {1}",
WriteLine("Upper bound of array {0} is: {1}",
arg0: array,
arg1: jagged[array].GetUpperBound(0));
}
@ -65,6 +65,6 @@ for (int row = 0; row <= jagged.GetUpperBound(0); row++)
{
for (int col = 0; col <= jagged[row].GetUpperBound(0); col++)
{
Console.WriteLine($"Row {row}, Column {col}: {jagged[row][col]}");
WriteLine($"Row {row}, Column {col}: {jagged[row][col]}");
}
}

View file

@ -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>

View file

@ -0,0 +1,30 @@
int a = 10; // 00001010
int b = 6; // 00000110
WriteLine($"a = {a}");
WriteLine($"b = {b}");
WriteLine($"a & b = {a & b}"); // 2-bit column only
WriteLine($"a | b = {a | b}"); // 8, 4, and 2-bit columns
WriteLine($"a ^ b = {a ^ b}"); // 8 and 4-bit columns
// 01010000 left-shift a by three bit columns
WriteLine($"a << 3 = {a << 3}");
// multiply a by 8
WriteLine($"a * 8 = {a * 8}");
// 00000011 right-shift b by one bit column
WriteLine($"b >> 1 = {b >> 1}");
WriteLine();
WriteLine("Outputting integers as binary:");
WriteLine($"a = {ToBinaryString(a)}");
WriteLine($"b = {ToBinaryString(b)}");
WriteLine($"a & b = {ToBinaryString(a & b)}");
WriteLine($"a | b = {ToBinaryString(a | b)}");
WriteLine($"a ^ b = {ToBinaryString(a ^ b)}");
static string ToBinaryString(int value)
{
return Convert.ToString(value, toBase: 2).PadLeft(8, '0');
}

View file

@ -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>

View file

@ -0,0 +1,27 @@
bool a = true;
bool b = false;
WriteLine($"AND | a | b ");
WriteLine($"a | {a & a,-5} | {a & b,-5} ");
WriteLine($"b | {b & a,-5} | {b & b,-5} ");
WriteLine();
WriteLine($"OR | a | b ");
WriteLine($"a | {a | a,-5} | {a | b,-5} ");
WriteLine($"b | {b | a,-5} | {b | b,-5} ");
WriteLine();
WriteLine($"XOR | a | b ");
WriteLine($"a | {a ^ a,-5} | {a ^ b,-5} ");
WriteLine($"b | {b ^ a,-5} | {b ^ b,-5} ");
WriteLine();
WriteLine($"a & DoStuff() = {a & DoStuff()}");
WriteLine($"b & DoStuff() = {b & DoStuff()}");
WriteLine();
WriteLine($"a && DoStuff() = {a && DoStuff()}");
WriteLine($"b && DoStuff() = {b && DoStuff()}");
static bool DoStuff()
{
WriteLine("I am doing some stuff.");
return true;
}

View file

@ -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>

View file

@ -0,0 +1,92 @@
using static System.Convert;
int a = 10;
double b = a; // an int can be safely cast into a double
WriteLine(b);
double c = 9.8;
int d = (int)c;
WriteLine(d); // d is 9 losing the .8 part
long e = 5_000_000_000;
int f = (int)e;
WriteLine($"e is {e:N0} and f is {f:N0}");
e = long.MaxValue;
f = (int)e;
WriteLine($"e is {e:N0} and f is {f:N0}");
double g = 9.8;
int h = ToInt32(g); // a method of System.Convert
WriteLine($"g is {g} and h is {h}");
// Understanding the default rounding rules
double[] doubles = new[]
{ 9.49, 9.5, 9.51, 10.49, 10.5, 10.51 };
foreach (double n in doubles)
{
WriteLine($"ToInt32({n}) is {ToInt32(n)}");
}
// Taking control of rounding rules
foreach (double n in doubles)
{
WriteLine(format:
"Math.Round({0}, 0, MidpointRounding.AwayFromZero) is {1}",
arg0: n,
arg1: Math.Round(value: n, digits: 0,
mode: MidpointRounding.AwayFromZero));
}
// Converting from any type to a string
int number = 12;
WriteLine(number.ToString());
bool boolean = true;
WriteLine(boolean.ToString());
DateTime now = DateTime.Now;
WriteLine(now.ToString());
object me = new();
WriteLine(me.ToString());
// allocate array of 128 bytes
byte[] binaryObject = new byte[128];
// populate array with random bytes
Random.Shared.NextBytes(binaryObject);
WriteLine("Binary Object as bytes:");
for (int index = 0; index < binaryObject.Length; index++)
{
Write($"{binaryObject[index]:X} ");
}
WriteLine();
// convert to Base64 string and output as text
string encoded = ToBase64String(binaryObject);
WriteLine($"Binary Object as Base64: {encoded}");
int age = int.Parse("27");
DateTime birthday = DateTime.Parse("4 July 1980");
WriteLine($"I was born {age} years ago.");
WriteLine($"My birthday is {birthday}.");
WriteLine($"My birthday is {birthday:D}.");
Write("How many eggs are there? ");
string? input = ReadLine(); // or use "12" in notebook
if (int.TryParse(input, out int count))
{
WriteLine($"There are {count} eggs.");
}
else
{
WriteLine("I could not parse the input.");
}

View file

@ -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>

View file

@ -0,0 +1,15 @@
checked
{
try
{
int max = 500;
for (byte i = 0; i < max; i++)
{
WriteLine(i);
}
}
catch (OverflowException ex)
{
WriteLine($"{ex.GetType()} says {ex.Message}");
}
}

View file

@ -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>

View file

@ -0,0 +1,26 @@
for (int i = 1; i <= 100; i++)
{
if (i % 15 == 0)
{
Write("FizzBuzz");
}
else if (i % 5 == 0)
{
Write("Buzz");
}
else if (i % 3 == 0)
{
Write("Fizz");
}
else
{
Write(i);
}
// put a comma and space after every number except 100
if (i < 100) Write(", ");
// write a carriage-return after every ten numbers
if (i % 10 == 0) WriteLine();
}
WriteLine();

View file

@ -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>

View file

@ -0,0 +1,19 @@
Write("Enter a number between 0 and 255: ");
string n1 = ReadLine()!;
Write("Enter another number between 0 and 255: ");
string n2 = ReadLine()!;
try
{
byte a = byte.Parse(n1);
byte b = byte.Parse(n2);
int answer = a / b;
WriteLine($"{a} divided by {b} is {answer}");
}
catch (Exception ex)
{
WriteLine($"{ex.GetType().Name}: {ex.Message}");
}

View file

@ -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>

View file

@ -0,0 +1,23 @@
WriteLine("int x = 3;");
WriteLine("int y = 2 + ++x;");
int x = 3;
int y = 2 + ++x;
WriteLine($"x = {x} and y = {y}");
WriteLine();
WriteLine("x = 3 << 2;");
WriteLine("y = 10 >> 1;");
x = 3 << 2;
y = 10 >> 1; ;
WriteLine($"x = {x} and y = {y}");
WriteLine();
WriteLine("x = 10 & 8;");
WriteLine("y = 10 | 7;");
x = 10 & 8;
y = 10 | 7;
WriteLine($"x = {x} and y = {y}");
WriteLine();

View file

@ -0,0 +1,43 @@
{
"folders": [
{
"path": "Arrays"
},
{
"path": "BitwiseAndShiftOperators"
},
{
"path": "BooleanOperators"
},
{
"path": "CastingConverting"
},
{
"path": "CheckingForOverflow"
},
{
"path": "HandlingExceptions"
},
{
"path": "IterationStatements"
},
{
"path": "Operators"
},
{
"path": "SelectionStatements"
},
{
"path": "Ch03Ex02LoopsAndOverflow"
},
{
"path": "Ch03Ex03FizzBuzz"
},
{
"path": "Ch03Ex04Exceptions"
},
{
"path": "Ch03Ex05Operators"
}
]
}

View file

@ -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>

View file

@ -0,0 +1,28 @@
try
{
checked
{
int x = int.MaxValue - 1;
WriteLine($"Initial value: {x}");
x++;
WriteLine($"After incrementing: {x}");
x++;
WriteLine($"After incrementing: {x}");
x++;
WriteLine($"After incrementing: {x}");
}
}
catch (OverflowException)
{
WriteLine("The code overflowed but I caught the exception.");
}
unchecked
{
int y = int.MaxValue + 1;
WriteLine($"Initial value: {y}");
y--;
WriteLine($"After decrementing: {y}");
y--;
WriteLine($"After decrementing: {y}");
}

View file

@ -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>

View file

@ -0,0 +1,40 @@
WriteLine("Before parsing");
Write("What is your age? ");
string? input = ReadLine(); // or use "49" in a notebook
try
{
int age = int.Parse(input!);
WriteLine($"You are {age} years old.");
}
catch (OverflowException)
{
WriteLine("Your age is a valid number format but it is either too big or small.");
}
catch (FormatException)
{
WriteLine("The age you entered is not a valid number format.");
}
catch (Exception ex)
{
WriteLine($"{ex.GetType()} says {ex.Message}");
}
WriteLine("After parsing");
Write("Enter an amount: ");
string amount = ReadLine()!;
if (string.IsNullOrEmpty(amount)) return;
try
{
decimal amountValue = decimal.Parse(amount);
WriteLine($"Amount formatted as currency: {amountValue:C}");
}
catch (FormatException) when (amount.Contains("$"))
{
WriteLine("Amounts cannot use the dollar sign!");
}
catch (FormatException)
{
WriteLine("Amounts must only contain digits!");
}

View file

@ -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>

View file

@ -0,0 +1,47 @@
// Looping with the while statement
int x = 0;
while (x < 10)
{
WriteLine(x);
x++;
}
// Looping with the do statement
string? password;
int attempts = 0;
do
{
attempts++;
Write("Enter your password: ");
password = ReadLine();
}
while ((password != "Pa$$w0rd") & (attempts < 10));
if (attempts < 10)
{
WriteLine("Correct!");
}
else
{
WriteLine("You have used 10 attempts!");
}
// Looping with the for statement
for (int y = 1; y <= 10; y++)
{
WriteLine(y);
}
// Looping with the foreach statement
string[] names = { "Adam", "Barry", "Charlie" };
foreach (string name in names)
{
WriteLine($"{name} has {name.Length} characters.");
}

View file

@ -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>

View file

@ -0,0 +1,20 @@
int a = 3;
int b = a++;
WriteLine($"a is {a}, b is {b}");
int c = 3;
int d = ++c; // increment c before assigning it
WriteLine($"c is {c}, d is {d}");
int e = 11;
int f = 3;
WriteLine($"e is {e}, f is {f}");
WriteLine($"e + f = {e + f}");
WriteLine($"e - f = {e - f}");
WriteLine($"e * f = {e * f}");
WriteLine($"e / f = {e / f}");
WriteLine($"e % f = {e % f}");
double g = 11.0;
WriteLine($"g is {g:N1}, f is {f}");
WriteLine($"g / f = {g / f}");

View 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);

View file

@ -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>