mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-04 13:57:37 +00:00
Initial commit
This commit is contained in:
parent
d5bde3c775
commit
5fb8e6929b
33 changed files with 842 additions and 0 deletions
51
vscode/Chapter02/Variables/Program.cs
Normal file
51
vscode/Chapter02/Variables/Program.cs
Normal 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}");
|
||||
10
vscode/Chapter02/Variables/Variables.csproj
Normal file
10
vscode/Chapter02/Variables/Variables.csproj
Normal 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>
|
||||
Loading…
Add table
Add a link
Reference in a new issue