diff --git a/vs4win/Chapter02/Ch02Ex03Numbers/Ch02Ex03Numbers.csproj b/vs4win/Chapter02/Ch02Ex03Numbers/Ch02Ex03Numbers.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vs4win/Chapter02/Ch02Ex03Numbers/Ch02Ex03Numbers.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter02/Ch02Ex03Numbers/Program.cs b/vs4win/Chapter02/Ch02Ex03Numbers/Program.cs new file mode 100644 index 0000000..5e12b34 --- /dev/null +++ b/vs4win/Chapter02/Ch02Ex03Numbers/Program.cs @@ -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("--------------------------------------------------------------------------"); diff --git a/vs4win/Chapter02/Chapter02.sln b/vs4win/Chapter02/Chapter02.sln index 6b63704..6c70eeb 100644 --- a/vs4win/Chapter02/Chapter02.sln +++ b/vs4win/Chapter02/Chapter02.sln @@ -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 diff --git a/vs4win/Chapter03/Arrays/Arrays.csproj b/vs4win/Chapter03/Arrays/Arrays.csproj index 74abf5c..ebe527c 100644 --- a/vs4win/Chapter03/Arrays/Arrays.csproj +++ b/vs4win/Chapter03/Arrays/Arrays.csproj @@ -1,10 +1,14 @@ - - Exe - net6.0 - enable - enable - + + Exe + net6.0 + enable + enable + + + + + diff --git a/vs4win/Chapter03/Arrays/Program.cs b/vs4win/Chapter03/Arrays/Program.cs index 529600f..a978d04 100644 --- a/vs4win/Chapter03/Arrays/Program.cs +++ b/vs4win/Chapter03/Arrays/Program.cs @@ -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]}"); } } diff --git a/vs4win/Chapter03/BitwiseAndShiftOperators/BitwiseAndShiftOperators.csproj b/vs4win/Chapter03/BitwiseAndShiftOperators/BitwiseAndShiftOperators.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vs4win/Chapter03/BitwiseAndShiftOperators/BitwiseAndShiftOperators.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter03/BitwiseAndShiftOperators/Program.cs b/vs4win/Chapter03/BitwiseAndShiftOperators/Program.cs new file mode 100644 index 0000000..5d6c49e --- /dev/null +++ b/vs4win/Chapter03/BitwiseAndShiftOperators/Program.cs @@ -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'); +} diff --git a/vs4win/Chapter03/BooleanOperators/BooleanOperators.csproj b/vs4win/Chapter03/BooleanOperators/BooleanOperators.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vs4win/Chapter03/BooleanOperators/BooleanOperators.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter03/BooleanOperators/Program.cs b/vs4win/Chapter03/BooleanOperators/Program.cs new file mode 100644 index 0000000..953a883 --- /dev/null +++ b/vs4win/Chapter03/BooleanOperators/Program.cs @@ -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; +} \ No newline at end of file diff --git a/vs4win/Chapter03/CastingConverting/CastingConverting.csproj b/vs4win/Chapter03/CastingConverting/CastingConverting.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vs4win/Chapter03/CastingConverting/CastingConverting.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter03/CastingConverting/Program.cs b/vs4win/Chapter03/CastingConverting/Program.cs new file mode 100644 index 0000000..215e740 --- /dev/null +++ b/vs4win/Chapter03/CastingConverting/Program.cs @@ -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."); +} diff --git a/vs4win/Chapter03/Ch03Ex02LoopsAndOverflow/Ch03Ex02LoopsAndOverflow.csproj b/vs4win/Chapter03/Ch03Ex02LoopsAndOverflow/Ch03Ex02LoopsAndOverflow.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vs4win/Chapter03/Ch03Ex02LoopsAndOverflow/Ch03Ex02LoopsAndOverflow.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter03/Ch03Ex02LoopsAndOverflow/Program.cs b/vs4win/Chapter03/Ch03Ex02LoopsAndOverflow/Program.cs new file mode 100644 index 0000000..0653abb --- /dev/null +++ b/vs4win/Chapter03/Ch03Ex02LoopsAndOverflow/Program.cs @@ -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}"); + } +} diff --git a/vs4win/Chapter03/Ch03Ex03FizzBuzz/Ch03Ex03FizzBuzz.csproj b/vs4win/Chapter03/Ch03Ex03FizzBuzz/Ch03Ex03FizzBuzz.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vs4win/Chapter03/Ch03Ex03FizzBuzz/Ch03Ex03FizzBuzz.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter03/Ch03Ex03FizzBuzz/Program.cs b/vs4win/Chapter03/Ch03Ex03FizzBuzz/Program.cs new file mode 100644 index 0000000..963e0b9 --- /dev/null +++ b/vs4win/Chapter03/Ch03Ex03FizzBuzz/Program.cs @@ -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(); diff --git a/vs4win/Chapter03/Ch03Ex04Exceptions/Ch03Ex04Exceptions.csproj b/vs4win/Chapter03/Ch03Ex04Exceptions/Ch03Ex04Exceptions.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vs4win/Chapter03/Ch03Ex04Exceptions/Ch03Ex04Exceptions.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter03/Ch03Ex04Exceptions/Program.cs b/vs4win/Chapter03/Ch03Ex04Exceptions/Program.cs new file mode 100644 index 0000000..8cac2a9 --- /dev/null +++ b/vs4win/Chapter03/Ch03Ex04Exceptions/Program.cs @@ -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}"); +} diff --git a/vs4win/Chapter03/Ch03Ex05Operators/Ch03Ex05Operators.csproj b/vs4win/Chapter03/Ch03Ex05Operators/Ch03Ex05Operators.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vs4win/Chapter03/Ch03Ex05Operators/Ch03Ex05Operators.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter03/Ch03Ex05Operators/Program.cs b/vs4win/Chapter03/Ch03Ex05Operators/Program.cs new file mode 100644 index 0000000..b304536 --- /dev/null +++ b/vs4win/Chapter03/Ch03Ex05Operators/Program.cs @@ -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(); diff --git a/vs4win/Chapter03/Chapter03.sln b/vs4win/Chapter03/Chapter03.sln new file mode 100644 index 0000000..aacf5b6 --- /dev/null +++ b/vs4win/Chapter03/Chapter03.sln @@ -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 diff --git a/vs4win/Chapter03/CheckingForOverflow/CheckingForOverflow.csproj b/vs4win/Chapter03/CheckingForOverflow/CheckingForOverflow.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vs4win/Chapter03/CheckingForOverflow/CheckingForOverflow.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter03/CheckingForOverflow/Program.cs b/vs4win/Chapter03/CheckingForOverflow/Program.cs new file mode 100644 index 0000000..47b263b --- /dev/null +++ b/vs4win/Chapter03/CheckingForOverflow/Program.cs @@ -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}"); +} diff --git a/vs4win/Chapter03/HandlingExceptions/HandlingExceptions.csproj b/vs4win/Chapter03/HandlingExceptions/HandlingExceptions.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vs4win/Chapter03/HandlingExceptions/HandlingExceptions.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter03/HandlingExceptions/Program.cs b/vs4win/Chapter03/HandlingExceptions/Program.cs new file mode 100644 index 0000000..d6482d5 --- /dev/null +++ b/vs4win/Chapter03/HandlingExceptions/Program.cs @@ -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!"); +} diff --git a/vs4win/Chapter03/IterationStatements/IterationStatements.csproj b/vs4win/Chapter03/IterationStatements/IterationStatements.csproj new file mode 100644 index 0000000..3f419b7 --- /dev/null +++ b/vs4win/Chapter03/IterationStatements/IterationStatements.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter03/IterationStatements/Program.cs b/vs4win/Chapter03/IterationStatements/Program.cs new file mode 100644 index 0000000..567b7ed --- /dev/null +++ b/vs4win/Chapter03/IterationStatements/Program.cs @@ -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."); +} \ No newline at end of file diff --git a/vs4win/Chapter03/Operators/Operators.csproj b/vs4win/Chapter03/Operators/Operators.csproj new file mode 100644 index 0000000..3f419b7 --- /dev/null +++ b/vs4win/Chapter03/Operators/Operators.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter03/Operators/Program.cs b/vs4win/Chapter03/Operators/Program.cs new file mode 100644 index 0000000..cbce163 --- /dev/null +++ b/vs4win/Chapter03/Operators/Program.cs @@ -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}"); diff --git a/vs4win/Chapter03/SelectionStatements/Program.cs b/vs4win/Chapter03/SelectionStatements/Program.cs new file mode 100644 index 0000000..ab25862 --- /dev/null +++ b/vs4win/Chapter03/SelectionStatements/Program.cs @@ -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); diff --git a/vs4win/Chapter03/SelectionStatements/SelectionStatements.csproj b/vs4win/Chapter03/SelectionStatements/SelectionStatements.csproj new file mode 100644 index 0000000..3f419b7 --- /dev/null +++ b/vs4win/Chapter03/SelectionStatements/SelectionStatements.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter02/Ch02Ex03Numbers/Ch02Ex03Numbers.csproj b/vscode/Chapter02/Ch02Ex03Numbers/Ch02Ex03Numbers.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vscode/Chapter02/Ch02Ex03Numbers/Ch02Ex03Numbers.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter02/Ch02Ex03Numbers/Program.cs b/vscode/Chapter02/Ch02Ex03Numbers/Program.cs new file mode 100644 index 0000000..5e12b34 --- /dev/null +++ b/vscode/Chapter02/Ch02Ex03Numbers/Program.cs @@ -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("--------------------------------------------------------------------------"); diff --git a/vscode/Chapter02/Chapter02.code-workspace b/vscode/Chapter02/Chapter02.code-workspace index 56f471c..0066c58 100644 --- a/vscode/Chapter02/Chapter02.code-workspace +++ b/vscode/Chapter02/Chapter02.code-workspace @@ -17,6 +17,9 @@ }, { "path": "Vocabulary" + }, + { + "path": "Ch02Ex03Numbers" } ] } \ No newline at end of file diff --git a/vscode/Chapter03/Arrays/Arrays.csproj b/vscode/Chapter03/Arrays/Arrays.csproj index 74abf5c..ebe527c 100644 --- a/vscode/Chapter03/Arrays/Arrays.csproj +++ b/vscode/Chapter03/Arrays/Arrays.csproj @@ -1,10 +1,14 @@ - - Exe - net6.0 - enable - enable - + + Exe + net6.0 + enable + enable + + + + + diff --git a/vscode/Chapter03/Arrays/Program.cs b/vscode/Chapter03/Arrays/Program.cs index 529600f..a978d04 100644 --- a/vscode/Chapter03/Arrays/Program.cs +++ b/vscode/Chapter03/Arrays/Program.cs @@ -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]}"); } } diff --git a/vscode/Chapter03/BitwiseAndShiftOperators/BitwiseAndShiftOperators.csproj b/vscode/Chapter03/BitwiseAndShiftOperators/BitwiseAndShiftOperators.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vscode/Chapter03/BitwiseAndShiftOperators/BitwiseAndShiftOperators.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter03/BitwiseAndShiftOperators/Program.cs b/vscode/Chapter03/BitwiseAndShiftOperators/Program.cs new file mode 100644 index 0000000..5d6c49e --- /dev/null +++ b/vscode/Chapter03/BitwiseAndShiftOperators/Program.cs @@ -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'); +} diff --git a/vscode/Chapter03/BooleanOperators/BooleanOperators.csproj b/vscode/Chapter03/BooleanOperators/BooleanOperators.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vscode/Chapter03/BooleanOperators/BooleanOperators.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter03/BooleanOperators/Program.cs b/vscode/Chapter03/BooleanOperators/Program.cs new file mode 100644 index 0000000..953a883 --- /dev/null +++ b/vscode/Chapter03/BooleanOperators/Program.cs @@ -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; +} \ No newline at end of file diff --git a/vscode/Chapter03/CastingConverting/CastingConverting.csproj b/vscode/Chapter03/CastingConverting/CastingConverting.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vscode/Chapter03/CastingConverting/CastingConverting.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter03/CastingConverting/Program.cs b/vscode/Chapter03/CastingConverting/Program.cs new file mode 100644 index 0000000..215e740 --- /dev/null +++ b/vscode/Chapter03/CastingConverting/Program.cs @@ -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."); +} diff --git a/vscode/Chapter03/Ch03Ex02LoopsAndOverflow/Ch03Ex02LoopsAndOverflow.csproj b/vscode/Chapter03/Ch03Ex02LoopsAndOverflow/Ch03Ex02LoopsAndOverflow.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vscode/Chapter03/Ch03Ex02LoopsAndOverflow/Ch03Ex02LoopsAndOverflow.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter03/Ch03Ex02LoopsAndOverflow/Program.cs b/vscode/Chapter03/Ch03Ex02LoopsAndOverflow/Program.cs new file mode 100644 index 0000000..0653abb --- /dev/null +++ b/vscode/Chapter03/Ch03Ex02LoopsAndOverflow/Program.cs @@ -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}"); + } +} diff --git a/vscode/Chapter03/Ch03Ex03FizzBuzz/Ch03Ex03FizzBuzz.csproj b/vscode/Chapter03/Ch03Ex03FizzBuzz/Ch03Ex03FizzBuzz.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vscode/Chapter03/Ch03Ex03FizzBuzz/Ch03Ex03FizzBuzz.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter03/Ch03Ex03FizzBuzz/Program.cs b/vscode/Chapter03/Ch03Ex03FizzBuzz/Program.cs new file mode 100644 index 0000000..963e0b9 --- /dev/null +++ b/vscode/Chapter03/Ch03Ex03FizzBuzz/Program.cs @@ -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(); diff --git a/vscode/Chapter03/Ch03Ex04Exceptions/Ch03Ex04Exceptions.csproj b/vscode/Chapter03/Ch03Ex04Exceptions/Ch03Ex04Exceptions.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vscode/Chapter03/Ch03Ex04Exceptions/Ch03Ex04Exceptions.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter03/Ch03Ex04Exceptions/Program.cs b/vscode/Chapter03/Ch03Ex04Exceptions/Program.cs new file mode 100644 index 0000000..8cac2a9 --- /dev/null +++ b/vscode/Chapter03/Ch03Ex04Exceptions/Program.cs @@ -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}"); +} diff --git a/vscode/Chapter03/Ch03Ex05Operators/Ch03Ex05Operators.csproj b/vscode/Chapter03/Ch03Ex05Operators/Ch03Ex05Operators.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vscode/Chapter03/Ch03Ex05Operators/Ch03Ex05Operators.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter03/Ch03Ex05Operators/Program.cs b/vscode/Chapter03/Ch03Ex05Operators/Program.cs new file mode 100644 index 0000000..b304536 --- /dev/null +++ b/vscode/Chapter03/Ch03Ex05Operators/Program.cs @@ -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(); diff --git a/vscode/Chapter03/Chapter03.code-workspace b/vscode/Chapter03/Chapter03.code-workspace new file mode 100644 index 0000000..5d1cd81 --- /dev/null +++ b/vscode/Chapter03/Chapter03.code-workspace @@ -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" + } + ] +} \ No newline at end of file diff --git a/vscode/Chapter03/CheckingForOverflow/CheckingForOverflow.csproj b/vscode/Chapter03/CheckingForOverflow/CheckingForOverflow.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vscode/Chapter03/CheckingForOverflow/CheckingForOverflow.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter03/CheckingForOverflow/Program.cs b/vscode/Chapter03/CheckingForOverflow/Program.cs new file mode 100644 index 0000000..47b263b --- /dev/null +++ b/vscode/Chapter03/CheckingForOverflow/Program.cs @@ -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}"); +} diff --git a/vscode/Chapter03/HandlingExceptions/HandlingExceptions.csproj b/vscode/Chapter03/HandlingExceptions/HandlingExceptions.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vscode/Chapter03/HandlingExceptions/HandlingExceptions.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter03/HandlingExceptions/Program.cs b/vscode/Chapter03/HandlingExceptions/Program.cs new file mode 100644 index 0000000..d6482d5 --- /dev/null +++ b/vscode/Chapter03/HandlingExceptions/Program.cs @@ -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!"); +} diff --git a/vscode/Chapter03/IterationStatements/IterationStatements.csproj b/vscode/Chapter03/IterationStatements/IterationStatements.csproj new file mode 100644 index 0000000..3f419b7 --- /dev/null +++ b/vscode/Chapter03/IterationStatements/IterationStatements.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter03/IterationStatements/Program.cs b/vscode/Chapter03/IterationStatements/Program.cs new file mode 100644 index 0000000..567b7ed --- /dev/null +++ b/vscode/Chapter03/IterationStatements/Program.cs @@ -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."); +} \ No newline at end of file diff --git a/vscode/Chapter03/Operators/Operators.csproj b/vscode/Chapter03/Operators/Operators.csproj new file mode 100644 index 0000000..3f419b7 --- /dev/null +++ b/vscode/Chapter03/Operators/Operators.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter03/Operators/Program.cs b/vscode/Chapter03/Operators/Program.cs new file mode 100644 index 0000000..cbce163 --- /dev/null +++ b/vscode/Chapter03/Operators/Program.cs @@ -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}"); diff --git a/vscode/Chapter03/SelectionStatements/Program.cs b/vscode/Chapter03/SelectionStatements/Program.cs new file mode 100644 index 0000000..ab25862 --- /dev/null +++ b/vscode/Chapter03/SelectionStatements/Program.cs @@ -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); diff --git a/vscode/Chapter03/SelectionStatements/SelectionStatements.csproj b/vscode/Chapter03/SelectionStatements/SelectionStatements.csproj new file mode 100644 index 0000000..3f419b7 --- /dev/null +++ b/vscode/Chapter03/SelectionStatements/SelectionStatements.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + +