mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-04 22:07:39 +00:00
Initial commit
This commit is contained in:
parent
ca7684985d
commit
49bdd4dda1
60 changed files with 1527 additions and 30 deletions
14
vscode/Chapter03/CastingConverting/CastingConverting.csproj
Normal file
14
vscode/Chapter03/CastingConverting/CastingConverting.csproj
Normal 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>
|
||||
92
vscode/Chapter03/CastingConverting/Program.cs
Normal file
92
vscode/Chapter03/CastingConverting/Program.cs
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
using static System.Convert;
|
||||
|
||||
int a = 10;
|
||||
double b = a; // an int can be safely cast into a double
|
||||
WriteLine(b);
|
||||
|
||||
double c = 9.8;
|
||||
int d = (int)c;
|
||||
WriteLine(d); // d is 9 losing the .8 part
|
||||
|
||||
long e = 5_000_000_000;
|
||||
int f = (int)e;
|
||||
WriteLine($"e is {e:N0} and f is {f:N0}");
|
||||
|
||||
e = long.MaxValue;
|
||||
f = (int)e;
|
||||
WriteLine($"e is {e:N0} and f is {f:N0}");
|
||||
|
||||
double g = 9.8;
|
||||
int h = ToInt32(g); // a method of System.Convert
|
||||
WriteLine($"g is {g} and h is {h}");
|
||||
|
||||
// Understanding the default rounding rules
|
||||
|
||||
double[] doubles = new[]
|
||||
{ 9.49, 9.5, 9.51, 10.49, 10.5, 10.51 };
|
||||
|
||||
foreach (double n in doubles)
|
||||
{
|
||||
WriteLine($"ToInt32({n}) is {ToInt32(n)}");
|
||||
}
|
||||
|
||||
// Taking control of rounding rules
|
||||
|
||||
foreach (double n in doubles)
|
||||
{
|
||||
WriteLine(format:
|
||||
"Math.Round({0}, 0, MidpointRounding.AwayFromZero) is {1}",
|
||||
arg0: n,
|
||||
arg1: Math.Round(value: n, digits: 0,
|
||||
mode: MidpointRounding.AwayFromZero));
|
||||
}
|
||||
|
||||
// Converting from any type to a string
|
||||
|
||||
int number = 12;
|
||||
WriteLine(number.ToString());
|
||||
|
||||
bool boolean = true;
|
||||
WriteLine(boolean.ToString());
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
WriteLine(now.ToString());
|
||||
|
||||
object me = new();
|
||||
WriteLine(me.ToString());
|
||||
|
||||
// allocate array of 128 bytes
|
||||
byte[] binaryObject = new byte[128];
|
||||
|
||||
// populate array with random bytes
|
||||
Random.Shared.NextBytes(binaryObject);
|
||||
|
||||
WriteLine("Binary Object as bytes:");
|
||||
for (int index = 0; index < binaryObject.Length; index++)
|
||||
{
|
||||
Write($"{binaryObject[index]:X} ");
|
||||
}
|
||||
WriteLine();
|
||||
|
||||
// convert to Base64 string and output as text
|
||||
string encoded = ToBase64String(binaryObject);
|
||||
WriteLine($"Binary Object as Base64: {encoded}");
|
||||
|
||||
int age = int.Parse("27");
|
||||
DateTime birthday = DateTime.Parse("4 July 1980");
|
||||
|
||||
WriteLine($"I was born {age} years ago.");
|
||||
WriteLine($"My birthday is {birthday}.");
|
||||
WriteLine($"My birthday is {birthday:D}.");
|
||||
|
||||
Write("How many eggs are there? ");
|
||||
string? input = ReadLine(); // or use "12" in notebook
|
||||
|
||||
if (int.TryParse(input, out int count))
|
||||
{
|
||||
WriteLine($"There are {count} eggs.");
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteLine("I could not parse the input.");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue