From 5fb8e6929bcce2b1c340bc2f058e47b353fe6ca5 Mon Sep 17 00:00:00 2001 From: Mark J Price Date: Sun, 13 Feb 2022 16:03:32 +0000 Subject: [PATCH] Initial commit --- vs4win/Chapter02/Arguments/Arguments.csproj | 15 ++++ vs4win/Chapter02/Arguments/Program.cs | 32 +++++++++ .../Arguments/Properties/launchSettings.json | 8 +++ vs4win/Chapter02/Arrays/Arrays.csproj | 10 +++ vs4win/Chapter02/Arrays/Program.cs | 70 +++++++++++++++++++ .../AsyncConsole/AsyncConsole.csproj | 15 ++++ vs4win/Chapter02/AsyncConsole/Program.cs | 7 ++ vs4win/Chapter02/Chapter02.sln | 61 ++++++++++++++++ vs4win/Chapter02/Formatting/Formatting.csproj | 14 ++++ vs4win/Chapter02/Formatting/Program.cs | 35 ++++++++++ vs4win/Chapter02/Numbers/Numbers.csproj | 10 +++ vs4win/Chapter02/Numbers/Program.cs | 52 ++++++++++++++ vs4win/Chapter02/Variables/Program.cs | 51 ++++++++++++++ vs4win/Chapter02/Variables/Variables.csproj | 10 +++ vs4win/Chapter02/Vocabulary/Program.cs | 34 +++++++++ vs4win/Chapter02/Vocabulary/Vocabulary.csproj | 15 ++++ vs4win/Chapter02/ch02.txt | 0 vscode/Chapter02/Arguments/Arguments.csproj | 15 ++++ vscode/Chapter02/Arguments/Program.cs | 32 +++++++++ .../Arguments/Properties/launchSettings.json | 8 +++ vscode/Chapter02/Arrays/Arrays.csproj | 10 +++ vscode/Chapter02/Arrays/Program.cs | 70 +++++++++++++++++++ .../AsyncConsole/AsyncConsole.csproj | 15 ++++ vscode/Chapter02/AsyncConsole/Program.cs | 7 ++ vscode/Chapter02/Chapter02.code-workspace | 25 +++++++ vscode/Chapter02/Formatting/Formatting.csproj | 14 ++++ vscode/Chapter02/Formatting/Program.cs | 35 ++++++++++ vscode/Chapter02/Numbers/Numbers.csproj | 10 +++ vscode/Chapter02/Numbers/Program.cs | 52 ++++++++++++++ vscode/Chapter02/Variables/Program.cs | 51 ++++++++++++++ vscode/Chapter02/Variables/Variables.csproj | 10 +++ vscode/Chapter02/Vocabulary/Program.cs | 34 +++++++++ vscode/Chapter02/Vocabulary/Vocabulary.csproj | 15 ++++ 33 files changed, 842 insertions(+) create mode 100644 vs4win/Chapter02/Arguments/Arguments.csproj create mode 100644 vs4win/Chapter02/Arguments/Program.cs create mode 100644 vs4win/Chapter02/Arguments/Properties/launchSettings.json create mode 100644 vs4win/Chapter02/Arrays/Arrays.csproj create mode 100644 vs4win/Chapter02/Arrays/Program.cs create mode 100644 vs4win/Chapter02/AsyncConsole/AsyncConsole.csproj create mode 100644 vs4win/Chapter02/AsyncConsole/Program.cs create mode 100644 vs4win/Chapter02/Chapter02.sln create mode 100644 vs4win/Chapter02/Formatting/Formatting.csproj create mode 100644 vs4win/Chapter02/Formatting/Program.cs create mode 100644 vs4win/Chapter02/Numbers/Numbers.csproj create mode 100644 vs4win/Chapter02/Numbers/Program.cs create mode 100644 vs4win/Chapter02/Variables/Program.cs create mode 100644 vs4win/Chapter02/Variables/Variables.csproj create mode 100644 vs4win/Chapter02/Vocabulary/Program.cs create mode 100644 vs4win/Chapter02/Vocabulary/Vocabulary.csproj delete mode 100644 vs4win/Chapter02/ch02.txt create mode 100644 vscode/Chapter02/Arguments/Arguments.csproj create mode 100644 vscode/Chapter02/Arguments/Program.cs create mode 100644 vscode/Chapter02/Arguments/Properties/launchSettings.json create mode 100644 vscode/Chapter02/Arrays/Arrays.csproj create mode 100644 vscode/Chapter02/Arrays/Program.cs create mode 100644 vscode/Chapter02/AsyncConsole/AsyncConsole.csproj create mode 100644 vscode/Chapter02/AsyncConsole/Program.cs create mode 100644 vscode/Chapter02/Chapter02.code-workspace create mode 100644 vscode/Chapter02/Formatting/Formatting.csproj create mode 100644 vscode/Chapter02/Formatting/Program.cs create mode 100644 vscode/Chapter02/Numbers/Numbers.csproj create mode 100644 vscode/Chapter02/Numbers/Program.cs create mode 100644 vscode/Chapter02/Variables/Program.cs create mode 100644 vscode/Chapter02/Variables/Variables.csproj create mode 100644 vscode/Chapter02/Vocabulary/Program.cs create mode 100644 vscode/Chapter02/Vocabulary/Vocabulary.csproj diff --git a/vs4win/Chapter02/Arguments/Arguments.csproj b/vs4win/Chapter02/Arguments/Arguments.csproj new file mode 100644 index 0000000..df2ad2f --- /dev/null +++ b/vs4win/Chapter02/Arguments/Arguments.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + diff --git a/vs4win/Chapter02/Arguments/Program.cs b/vs4win/Chapter02/Arguments/Program.cs new file mode 100644 index 0000000..7b11202 --- /dev/null +++ b/vs4win/Chapter02/Arguments/Program.cs @@ -0,0 +1,32 @@ +WriteLine($"There are {args.Length} arguments."); + +foreach (string arg in args) +{ + WriteLine(arg); +} + +if (args.Length < 3) +{ + WriteLine("You must specify two colors and cursor size, e.g."); + WriteLine("dotnet run red yellow 50"); + return; // stop running +} + +ForegroundColor = (ConsoleColor)Enum.Parse( + enumType: typeof(ConsoleColor), + value: args[0], + ignoreCase: true); + +BackgroundColor = (ConsoleColor)Enum.Parse( + enumType: typeof(ConsoleColor), + value: args[1], + ignoreCase: true); + +try +{ + CursorSize = int.Parse(args[2]); +} +catch (PlatformNotSupportedException) +{ + WriteLine("The current platform does not support changing the size of the cursor."); +} diff --git a/vs4win/Chapter02/Arguments/Properties/launchSettings.json b/vs4win/Chapter02/Arguments/Properties/launchSettings.json new file mode 100644 index 0000000..aef3907 --- /dev/null +++ b/vs4win/Chapter02/Arguments/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "Arguments": { + "commandName": "Project", + "commandLineArgs": "red yellow 50" + } + } +} \ No newline at end of file diff --git a/vs4win/Chapter02/Arrays/Arrays.csproj b/vs4win/Chapter02/Arrays/Arrays.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/vs4win/Chapter02/Arrays/Arrays.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/vs4win/Chapter02/Arrays/Program.cs b/vs4win/Chapter02/Arrays/Program.cs new file mode 100644 index 0000000..529600f --- /dev/null +++ b/vs4win/Chapter02/Arrays/Program.cs @@ -0,0 +1,70 @@ +string[] names; // can reference any size array of strings + +// allocating memory for four strings in an array +names = new string[4]; + +// storing items at index positions +names[0] = "Kate"; +names[1] = "Jack"; +names[2] = "Rebecca"; +names[3] = "Tom"; + +string[] names2 = new[] { "Kate", "Jack", "Rebecca", "Tom" }; + +// looping through the names +for (int i = 0; i < names2.Length; i++) +{ + // output the item at index position i + Console.WriteLine(names2[i]); +} + +string[,] grid1 = new[,] // two dimensions +{ + { "Alpha", "Beta", "Gamma", "Delta" }, + { "Anne", "Ben", "Charlie", "Doug" }, + { "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)}"); + +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]}"); + } +} + +// alternative syntax +string[,] grid2 = new string[3,4]; // allocate memory +grid2[0, 0] = "Alpha"; // assign string values +// and so on +grid2[2, 3] = "Dog"; + +string[][] jagged = new[] // array of string arrays +{ + new[] { "Alpha", "Beta", "Gamma" }, + new[] { "Anne", "Ben", "Charlie", "Doug" }, + new[] { "Aardvark", "Bear" } +}; + +Console.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}", + arg0: array, + arg1: jagged[array].GetUpperBound(0)); +} + +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]}"); + } +} diff --git a/vs4win/Chapter02/AsyncConsole/AsyncConsole.csproj b/vs4win/Chapter02/AsyncConsole/AsyncConsole.csproj new file mode 100644 index 0000000..df2ad2f --- /dev/null +++ b/vs4win/Chapter02/AsyncConsole/AsyncConsole.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + diff --git a/vs4win/Chapter02/AsyncConsole/Program.cs b/vs4win/Chapter02/AsyncConsole/Program.cs new file mode 100644 index 0000000..bde47bf --- /dev/null +++ b/vs4win/Chapter02/AsyncConsole/Program.cs @@ -0,0 +1,7 @@ +HttpClient client = new(); + +HttpResponseMessage response = + await client.GetAsync("http://www.apple.com/"); + +WriteLine("Apple's home page has {0:N0} bytes.", + response.Content.Headers.ContentLength); \ No newline at end of file diff --git a/vs4win/Chapter02/Chapter02.sln b/vs4win/Chapter02/Chapter02.sln new file mode 100644 index 0000000..1fbc7a3 --- /dev/null +++ b/vs4win/Chapter02/Chapter02.sln @@ -0,0 +1,61 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.32126.317 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Vocabulary", "Vocabulary\Vocabulary.csproj", "{0CE59DD7-3883-4367-B69E-8C72A4347291}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Numbers", "Numbers\Numbers.csproj", "{830F8E78-582E-48E5-97D3-937218DBCAA9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Variables", "Variables\Variables.csproj", "{ACF3C314-6997-4613-93ED-B657713DB931}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Arrays", "Arrays\Arrays.csproj", "{392E45A4-7C81-4435-A0C5-B66D7CF27B78}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Formatting", "Formatting\Formatting.csproj", "{71E2E795-978C-4BD8-8DD7-C6B1FEC04818}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Arguments", "Arguments\Arguments.csproj", "{2293F4A7-AC7E-4A9D-BEAB-A6182D3D66EC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncConsole", "AsyncConsole\AsyncConsole.csproj", "{459F1756-610C-4C65-9335-C269DE60AFD3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0CE59DD7-3883-4367-B69E-8C72A4347291}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0CE59DD7-3883-4367-B69E-8C72A4347291}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0CE59DD7-3883-4367-B69E-8C72A4347291}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0CE59DD7-3883-4367-B69E-8C72A4347291}.Release|Any CPU.Build.0 = Release|Any CPU + {830F8E78-582E-48E5-97D3-937218DBCAA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {830F8E78-582E-48E5-97D3-937218DBCAA9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {830F8E78-582E-48E5-97D3-937218DBCAA9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {830F8E78-582E-48E5-97D3-937218DBCAA9}.Release|Any CPU.Build.0 = Release|Any CPU + {ACF3C314-6997-4613-93ED-B657713DB931}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ACF3C314-6997-4613-93ED-B657713DB931}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ACF3C314-6997-4613-93ED-B657713DB931}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ACF3C314-6997-4613-93ED-B657713DB931}.Release|Any CPU.Build.0 = Release|Any CPU + {392E45A4-7C81-4435-A0C5-B66D7CF27B78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {392E45A4-7C81-4435-A0C5-B66D7CF27B78}.Debug|Any CPU.Build.0 = Debug|Any CPU + {392E45A4-7C81-4435-A0C5-B66D7CF27B78}.Release|Any CPU.ActiveCfg = Release|Any CPU + {392E45A4-7C81-4435-A0C5-B66D7CF27B78}.Release|Any CPU.Build.0 = Release|Any CPU + {71E2E795-978C-4BD8-8DD7-C6B1FEC04818}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {71E2E795-978C-4BD8-8DD7-C6B1FEC04818}.Debug|Any CPU.Build.0 = Debug|Any CPU + {71E2E795-978C-4BD8-8DD7-C6B1FEC04818}.Release|Any CPU.ActiveCfg = Release|Any CPU + {71E2E795-978C-4BD8-8DD7-C6B1FEC04818}.Release|Any CPU.Build.0 = Release|Any CPU + {2293F4A7-AC7E-4A9D-BEAB-A6182D3D66EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2293F4A7-AC7E-4A9D-BEAB-A6182D3D66EC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2293F4A7-AC7E-4A9D-BEAB-A6182D3D66EC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2293F4A7-AC7E-4A9D-BEAB-A6182D3D66EC}.Release|Any CPU.Build.0 = Release|Any CPU + {459F1756-610C-4C65-9335-C269DE60AFD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {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 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E88CEC15-5E2F-4B39-9286-76AE13BEF040} + EndGlobalSection +EndGlobal diff --git a/vs4win/Chapter02/Formatting/Formatting.csproj b/vs4win/Chapter02/Formatting/Formatting.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vs4win/Chapter02/Formatting/Formatting.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vs4win/Chapter02/Formatting/Program.cs b/vs4win/Chapter02/Formatting/Program.cs new file mode 100644 index 0000000..614b304 --- /dev/null +++ b/vs4win/Chapter02/Formatting/Program.cs @@ -0,0 +1,35 @@ +using static System.Console; + +int numberOfApples = 12; +decimal pricePerApple = 0.35M; + +WriteLine( + format: "{0} apples costs {1:C}", + arg0: numberOfApples, + arg1: pricePerApple * numberOfApples); + +string formatted = string.Format( + format: "{0} apples costs {1:C}", + arg0: numberOfApples, + arg1: pricePerApple * numberOfApples); + +//WriteToFile(formatted); // writes the string into a file + +WriteLine($"{numberOfApples} apples costs {pricePerApple * numberOfApples:C}"); + +Write("Type your first name and press ENTER: "); +string? firstName = ReadLine(); // nulls are expected + +Write("Type your age and press ENTER: "); +string age = ReadLine()!; // null won't be returned + +WriteLine( + $"Hello {firstName}, you look good for {age}."); + +Write("Press any key combination: "); +ConsoleKeyInfo key = ReadKey(); +WriteLine(); +WriteLine("Key: {0}, Char: {1}, Modifiers: {2}", + arg0: key.Key, + arg1: key.KeyChar, + arg2: key.Modifiers); diff --git a/vs4win/Chapter02/Numbers/Numbers.csproj b/vs4win/Chapter02/Numbers/Numbers.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/vs4win/Chapter02/Numbers/Numbers.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/vs4win/Chapter02/Numbers/Program.cs b/vs4win/Chapter02/Numbers/Program.cs new file mode 100644 index 0000000..940e7d0 --- /dev/null +++ b/vs4win/Chapter02/Numbers/Program.cs @@ -0,0 +1,52 @@ +// unsigned integer means positive whole number or 0 +uint naturalNumber = 23; + +// integer means negative or positive whole number or 0 +int integerNumber = -23; + +// float means single-precision floating point +// F suffix makes it a float literal +float realNumber = 2.3F; + +// double means double-precision floating point +double anotherRealNumber = 2.3; // double literal + +// three variables that store the number 2 million +int decimalNotation = 2_000_000; +int binaryNotation = 0b_0001_1110_1000_0100_1000_0000; +int hexadecimalNotation = 0x_001E_8480; + +// check the three variables have the same value +// both statements output true +Console.WriteLine($"{decimalNotation == binaryNotation}"); +Console.WriteLine( + $"{decimalNotation == hexadecimalNotation}"); + +Console.WriteLine($"int uses {sizeof(int)} bytes and can store numbers in the range {int.MinValue:N0} to {int.MaxValue:N0}."); +Console.WriteLine($"double uses {sizeof(double)} bytes and can store numbers in the range {double.MinValue:N0} to {double.MaxValue:N0}."); +Console.WriteLine($"decimal uses {sizeof(decimal)} bytes and can store numbers in the range {decimal.MinValue:N0} to {decimal.MaxValue:N0}."); + +Console.WriteLine("Using doubles:"); +double a = 0.1; +double b = 0.2; +if (a + b == 0.3) +{ + Console.WriteLine($"{a} + {b} equals {0.3}"); +} +else +{ + Console.WriteLine($"{a} + {b} does NOT equal {0.3}"); +} + +Console.WriteLine("Using decimals:"); +decimal c = 0.1M; // M suffix means a decimal literal value +decimal d = 0.2M; + +if (c + d == 0.3M) +{ + Console.WriteLine($"{c} + {d} equals {0.3M}"); +} +else +{ + Console.WriteLine($"{c} + {d} does NOT equal {0.3M}"); +} diff --git a/vs4win/Chapter02/Variables/Program.cs b/vs4win/Chapter02/Variables/Program.cs new file mode 100644 index 0000000..7406b76 --- /dev/null +++ b/vs4win/Chapter02/Variables/Program.cs @@ -0,0 +1,51 @@ +using System.Xml; + +object height = 1.88; // storing a double in an object +object name = "Amir"; // storing a string in an object +Console.WriteLine($"{name} is {height} metres tall."); + +//int length1 = name.Length; // gives compile error! +int length2 = ((string)name).Length; // tell compiler it is a string +Console.WriteLine($"{name} has {length2} characters."); + +// storing a string in a dynamic object +// string has a Length property +dynamic something = "Ahmed"; + +// int does not have a Length property +something = 12; +// an array of any type has a Length property +something = new[] { 3, 5, 7 }; + +// this compiles but would throw an exception at run-time +// if you later store a data type that does not have a +// property named Length +Console.WriteLine($"Length is {something.Length}"); + +var population = 66_000_000; // 66 million in UK +var weight = 1.88; // in kilograms +var price = 4.99M; // in pounds sterling +var fruit = "Apples"; // strings use double-quotes +var letter = 'Z'; // chars use single-quotes +var happy = true; // Booleans have value of true or false + +// good use of var because it avoids the repeated type +// as shown in the more verbose second statement +var xml1 = new XmlDocument(); // C# 3 and later +XmlDocument xml2 = new XmlDocument(); // all C# versions + +// bad use of var because we cannot tell the type, so we +// should use a specific type declaration as shown in +// the second statement +var file1 = File.CreateText("something1.txt"); +StreamWriter file2 = File.CreateText("something2.txt"); + +Console.WriteLine($"default(int) = {default(int)}"); +Console.WriteLine($"default(bool) = {default(bool)}"); +Console.WriteLine($"default(DateTime) = {default(DateTime)}"); +Console.WriteLine($"default(string) = {default(string)}"); + +int number = 13; +Console.WriteLine($"number has been set to: {number}"); +number = default; +Console.WriteLine($"number has been reset to its default: {number}"); diff --git a/vs4win/Chapter02/Variables/Variables.csproj b/vs4win/Chapter02/Variables/Variables.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/vs4win/Chapter02/Variables/Variables.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/vs4win/Chapter02/Vocabulary/Program.cs b/vs4win/Chapter02/Vocabulary/Program.cs new file mode 100644 index 0000000..0b51f6a --- /dev/null +++ b/vs4win/Chapter02/Vocabulary/Program.cs @@ -0,0 +1,34 @@ +using System.Reflection; + +// declare some unused variables using types +// in additional assemblies +System.Data.DataSet ds; +HttpClient client; + +Assembly? myApp = Assembly.GetEntryAssembly(); + +if (myApp == null) return; // quit the app + +// loop through the assemblies that my app references +foreach (AssemblyName name in myApp.GetReferencedAssemblies()) +{ + // load the assembly so we can read its details + Assembly a = Assembly.Load(name); + + // declare a variable to count the number of methods + int methodCount = 0; + + // loop through all the types in the assembly + foreach (TypeInfo t in a.DefinedTypes) + { + // add up the counts of methods + methodCount += t.GetMethods().Count(); + } + + // output the count of types and their methods + Console.WriteLine( + "{0:N0} types with {1:N0} methods in {2} assembly.", + arg0: a.DefinedTypes.Count(), + arg1: methodCount, + arg2: name.Name); +} \ No newline at end of file diff --git a/vs4win/Chapter02/Vocabulary/Vocabulary.csproj b/vs4win/Chapter02/Vocabulary/Vocabulary.csproj new file mode 100644 index 0000000..741fa15 --- /dev/null +++ b/vs4win/Chapter02/Vocabulary/Vocabulary.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + diff --git a/vs4win/Chapter02/ch02.txt b/vs4win/Chapter02/ch02.txt deleted file mode 100644 index e69de29..0000000 diff --git a/vscode/Chapter02/Arguments/Arguments.csproj b/vscode/Chapter02/Arguments/Arguments.csproj new file mode 100644 index 0000000..df2ad2f --- /dev/null +++ b/vscode/Chapter02/Arguments/Arguments.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + diff --git a/vscode/Chapter02/Arguments/Program.cs b/vscode/Chapter02/Arguments/Program.cs new file mode 100644 index 0000000..7b11202 --- /dev/null +++ b/vscode/Chapter02/Arguments/Program.cs @@ -0,0 +1,32 @@ +WriteLine($"There are {args.Length} arguments."); + +foreach (string arg in args) +{ + WriteLine(arg); +} + +if (args.Length < 3) +{ + WriteLine("You must specify two colors and cursor size, e.g."); + WriteLine("dotnet run red yellow 50"); + return; // stop running +} + +ForegroundColor = (ConsoleColor)Enum.Parse( + enumType: typeof(ConsoleColor), + value: args[0], + ignoreCase: true); + +BackgroundColor = (ConsoleColor)Enum.Parse( + enumType: typeof(ConsoleColor), + value: args[1], + ignoreCase: true); + +try +{ + CursorSize = int.Parse(args[2]); +} +catch (PlatformNotSupportedException) +{ + WriteLine("The current platform does not support changing the size of the cursor."); +} diff --git a/vscode/Chapter02/Arguments/Properties/launchSettings.json b/vscode/Chapter02/Arguments/Properties/launchSettings.json new file mode 100644 index 0000000..aef3907 --- /dev/null +++ b/vscode/Chapter02/Arguments/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "Arguments": { + "commandName": "Project", + "commandLineArgs": "red yellow 50" + } + } +} \ No newline at end of file diff --git a/vscode/Chapter02/Arrays/Arrays.csproj b/vscode/Chapter02/Arrays/Arrays.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/vscode/Chapter02/Arrays/Arrays.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/vscode/Chapter02/Arrays/Program.cs b/vscode/Chapter02/Arrays/Program.cs new file mode 100644 index 0000000..529600f --- /dev/null +++ b/vscode/Chapter02/Arrays/Program.cs @@ -0,0 +1,70 @@ +string[] names; // can reference any size array of strings + +// allocating memory for four strings in an array +names = new string[4]; + +// storing items at index positions +names[0] = "Kate"; +names[1] = "Jack"; +names[2] = "Rebecca"; +names[3] = "Tom"; + +string[] names2 = new[] { "Kate", "Jack", "Rebecca", "Tom" }; + +// looping through the names +for (int i = 0; i < names2.Length; i++) +{ + // output the item at index position i + Console.WriteLine(names2[i]); +} + +string[,] grid1 = new[,] // two dimensions +{ + { "Alpha", "Beta", "Gamma", "Delta" }, + { "Anne", "Ben", "Charlie", "Doug" }, + { "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)}"); + +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]}"); + } +} + +// alternative syntax +string[,] grid2 = new string[3,4]; // allocate memory +grid2[0, 0] = "Alpha"; // assign string values +// and so on +grid2[2, 3] = "Dog"; + +string[][] jagged = new[] // array of string arrays +{ + new[] { "Alpha", "Beta", "Gamma" }, + new[] { "Anne", "Ben", "Charlie", "Doug" }, + new[] { "Aardvark", "Bear" } +}; + +Console.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}", + arg0: array, + arg1: jagged[array].GetUpperBound(0)); +} + +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]}"); + } +} diff --git a/vscode/Chapter02/AsyncConsole/AsyncConsole.csproj b/vscode/Chapter02/AsyncConsole/AsyncConsole.csproj new file mode 100644 index 0000000..df2ad2f --- /dev/null +++ b/vscode/Chapter02/AsyncConsole/AsyncConsole.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + diff --git a/vscode/Chapter02/AsyncConsole/Program.cs b/vscode/Chapter02/AsyncConsole/Program.cs new file mode 100644 index 0000000..bde47bf --- /dev/null +++ b/vscode/Chapter02/AsyncConsole/Program.cs @@ -0,0 +1,7 @@ +HttpClient client = new(); + +HttpResponseMessage response = + await client.GetAsync("http://www.apple.com/"); + +WriteLine("Apple's home page has {0:N0} bytes.", + response.Content.Headers.ContentLength); \ No newline at end of file diff --git a/vscode/Chapter02/Chapter02.code-workspace b/vscode/Chapter02/Chapter02.code-workspace new file mode 100644 index 0000000..6b3dfb6 --- /dev/null +++ b/vscode/Chapter02/Chapter02.code-workspace @@ -0,0 +1,25 @@ +{ + "folders": [ + { + "path": "Arguments" + }, + { + "path": "Arrays" + }, + { + "path": "AsyncConsole" + }, + { + "path": "Formatting" + }, + { + "path": "Numbers" + }, + { + "path": "Variables" + }, + { + "path": "Vocabulary" + } + ] +} \ No newline at end of file diff --git a/vscode/Chapter02/Formatting/Formatting.csproj b/vscode/Chapter02/Formatting/Formatting.csproj new file mode 100644 index 0000000..ebe527c --- /dev/null +++ b/vscode/Chapter02/Formatting/Formatting.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter02/Formatting/Program.cs b/vscode/Chapter02/Formatting/Program.cs new file mode 100644 index 0000000..614b304 --- /dev/null +++ b/vscode/Chapter02/Formatting/Program.cs @@ -0,0 +1,35 @@ +using static System.Console; + +int numberOfApples = 12; +decimal pricePerApple = 0.35M; + +WriteLine( + format: "{0} apples costs {1:C}", + arg0: numberOfApples, + arg1: pricePerApple * numberOfApples); + +string formatted = string.Format( + format: "{0} apples costs {1:C}", + arg0: numberOfApples, + arg1: pricePerApple * numberOfApples); + +//WriteToFile(formatted); // writes the string into a file + +WriteLine($"{numberOfApples} apples costs {pricePerApple * numberOfApples:C}"); + +Write("Type your first name and press ENTER: "); +string? firstName = ReadLine(); // nulls are expected + +Write("Type your age and press ENTER: "); +string age = ReadLine()!; // null won't be returned + +WriteLine( + $"Hello {firstName}, you look good for {age}."); + +Write("Press any key combination: "); +ConsoleKeyInfo key = ReadKey(); +WriteLine(); +WriteLine("Key: {0}, Char: {1}, Modifiers: {2}", + arg0: key.Key, + arg1: key.KeyChar, + arg2: key.Modifiers); diff --git a/vscode/Chapter02/Numbers/Numbers.csproj b/vscode/Chapter02/Numbers/Numbers.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/vscode/Chapter02/Numbers/Numbers.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/vscode/Chapter02/Numbers/Program.cs b/vscode/Chapter02/Numbers/Program.cs new file mode 100644 index 0000000..940e7d0 --- /dev/null +++ b/vscode/Chapter02/Numbers/Program.cs @@ -0,0 +1,52 @@ +// unsigned integer means positive whole number or 0 +uint naturalNumber = 23; + +// integer means negative or positive whole number or 0 +int integerNumber = -23; + +// float means single-precision floating point +// F suffix makes it a float literal +float realNumber = 2.3F; + +// double means double-precision floating point +double anotherRealNumber = 2.3; // double literal + +// three variables that store the number 2 million +int decimalNotation = 2_000_000; +int binaryNotation = 0b_0001_1110_1000_0100_1000_0000; +int hexadecimalNotation = 0x_001E_8480; + +// check the three variables have the same value +// both statements output true +Console.WriteLine($"{decimalNotation == binaryNotation}"); +Console.WriteLine( + $"{decimalNotation == hexadecimalNotation}"); + +Console.WriteLine($"int uses {sizeof(int)} bytes and can store numbers in the range {int.MinValue:N0} to {int.MaxValue:N0}."); +Console.WriteLine($"double uses {sizeof(double)} bytes and can store numbers in the range {double.MinValue:N0} to {double.MaxValue:N0}."); +Console.WriteLine($"decimal uses {sizeof(decimal)} bytes and can store numbers in the range {decimal.MinValue:N0} to {decimal.MaxValue:N0}."); + +Console.WriteLine("Using doubles:"); +double a = 0.1; +double b = 0.2; +if (a + b == 0.3) +{ + Console.WriteLine($"{a} + {b} equals {0.3}"); +} +else +{ + Console.WriteLine($"{a} + {b} does NOT equal {0.3}"); +} + +Console.WriteLine("Using decimals:"); +decimal c = 0.1M; // M suffix means a decimal literal value +decimal d = 0.2M; + +if (c + d == 0.3M) +{ + Console.WriteLine($"{c} + {d} equals {0.3M}"); +} +else +{ + Console.WriteLine($"{c} + {d} does NOT equal {0.3M}"); +} diff --git a/vscode/Chapter02/Variables/Program.cs b/vscode/Chapter02/Variables/Program.cs new file mode 100644 index 0000000..7406b76 --- /dev/null +++ b/vscode/Chapter02/Variables/Program.cs @@ -0,0 +1,51 @@ +using System.Xml; + +object height = 1.88; // storing a double in an object +object name = "Amir"; // storing a string in an object +Console.WriteLine($"{name} is {height} metres tall."); + +//int length1 = name.Length; // gives compile error! +int length2 = ((string)name).Length; // tell compiler it is a string +Console.WriteLine($"{name} has {length2} characters."); + +// storing a string in a dynamic object +// string has a Length property +dynamic something = "Ahmed"; + +// int does not have a Length property +something = 12; +// an array of any type has a Length property +something = new[] { 3, 5, 7 }; + +// this compiles but would throw an exception at run-time +// if you later store a data type that does not have a +// property named Length +Console.WriteLine($"Length is {something.Length}"); + +var population = 66_000_000; // 66 million in UK +var weight = 1.88; // in kilograms +var price = 4.99M; // in pounds sterling +var fruit = "Apples"; // strings use double-quotes +var letter = 'Z'; // chars use single-quotes +var happy = true; // Booleans have value of true or false + +// good use of var because it avoids the repeated type +// as shown in the more verbose second statement +var xml1 = new XmlDocument(); // C# 3 and later +XmlDocument xml2 = new XmlDocument(); // all C# versions + +// bad use of var because we cannot tell the type, so we +// should use a specific type declaration as shown in +// the second statement +var file1 = File.CreateText("something1.txt"); +StreamWriter file2 = File.CreateText("something2.txt"); + +Console.WriteLine($"default(int) = {default(int)}"); +Console.WriteLine($"default(bool) = {default(bool)}"); +Console.WriteLine($"default(DateTime) = {default(DateTime)}"); +Console.WriteLine($"default(string) = {default(string)}"); + +int number = 13; +Console.WriteLine($"number has been set to: {number}"); +number = default; +Console.WriteLine($"number has been reset to its default: {number}"); diff --git a/vscode/Chapter02/Variables/Variables.csproj b/vscode/Chapter02/Variables/Variables.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/vscode/Chapter02/Variables/Variables.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/vscode/Chapter02/Vocabulary/Program.cs b/vscode/Chapter02/Vocabulary/Program.cs new file mode 100644 index 0000000..0b51f6a --- /dev/null +++ b/vscode/Chapter02/Vocabulary/Program.cs @@ -0,0 +1,34 @@ +using System.Reflection; + +// declare some unused variables using types +// in additional assemblies +System.Data.DataSet ds; +HttpClient client; + +Assembly? myApp = Assembly.GetEntryAssembly(); + +if (myApp == null) return; // quit the app + +// loop through the assemblies that my app references +foreach (AssemblyName name in myApp.GetReferencedAssemblies()) +{ + // load the assembly so we can read its details + Assembly a = Assembly.Load(name); + + // declare a variable to count the number of methods + int methodCount = 0; + + // loop through all the types in the assembly + foreach (TypeInfo t in a.DefinedTypes) + { + // add up the counts of methods + methodCount += t.GetMethods().Count(); + } + + // output the count of types and their methods + Console.WriteLine( + "{0:N0} types with {1:N0} methods in {2} assembly.", + arg0: a.DefinedTypes.Count(), + arg1: methodCount, + arg2: name.Name); +} \ No newline at end of file diff --git a/vscode/Chapter02/Vocabulary/Vocabulary.csproj b/vscode/Chapter02/Vocabulary/Vocabulary.csproj new file mode 100644 index 0000000..741fa15 --- /dev/null +++ b/vscode/Chapter02/Vocabulary/Vocabulary.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + +