Initial commit

This commit is contained in:
Mark J Price 2022-02-13 16:03:32 +00:00
parent d5bde3c775
commit 5fb8e6929b
33 changed files with 842 additions and 0 deletions

View file

@ -0,0 +1,15 @@
<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,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.");
}

View file

@ -0,0 +1,8 @@
{
"profiles": {
"Arguments": {
"commandName": "Project",
"commandLineArgs": "red yellow 50"
}
}
}

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -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]}");
}
}

View file

@ -0,0 +1,15 @@
<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,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);

View file

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

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

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -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}");
}

View file

@ -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}");

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -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);
}

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Using Remove="System.Threading" />
<Using Include="System.Numerics" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,15 @@
<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,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.");
}

View file

@ -0,0 +1,8 @@
{
"profiles": {
"Arguments": {
"commandName": "Project",
"commandLineArgs": "red yellow 50"
}
}
}

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -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]}");
}
}

View file

@ -0,0 +1,15 @@
<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,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);

View file

@ -0,0 +1,25 @@
{
"folders": [
{
"path": "Arguments"
},
{
"path": "Arrays"
},
{
"path": "AsyncConsole"
},
{
"path": "Formatting"
},
{
"path": "Numbers"
},
{
"path": "Variables"
},
{
"path": "Vocabulary"
}
]
}

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

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -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}");
}

View file

@ -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}");

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -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);
}

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Using Remove="System.Threading" />
<Using Include="System.Numerics" />
</ItemGroup>
</Project>