mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-06 06:43:47 +00:00
Initial commit
This commit is contained in:
parent
bc96ccb183
commit
18f89e91d4
62 changed files with 1661 additions and 2 deletions
29
vscode/Chapter06/NullHandling/Program.cs
Normal file
29
vscode/Chapter06/NullHandling/Program.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
int thisCannotBeNull = 4;
|
||||
//thisCannotBeNull = null; // compile error!
|
||||
WriteLine(thisCannotBeNull);
|
||||
|
||||
int? thisCouldBeNull = null;
|
||||
|
||||
WriteLine(thisCouldBeNull);
|
||||
WriteLine(thisCouldBeNull.GetValueOrDefault());
|
||||
|
||||
thisCouldBeNull = 7;
|
||||
|
||||
WriteLine(thisCouldBeNull);
|
||||
WriteLine(thisCouldBeNull.GetValueOrDefault());
|
||||
|
||||
// the actual type of int? is Nullable<int>
|
||||
Nullable<int> thisCouldAlsoBeNull = null;
|
||||
thisCouldAlsoBeNull = 9;
|
||||
WriteLine(thisCouldAlsoBeNull);
|
||||
|
||||
// Declaring non-nullable variables and parameters
|
||||
|
||||
Address address = new();
|
||||
address.Building = null;
|
||||
address.Street = null!; // null-forgiving operator
|
||||
address.City = "London";
|
||||
address.Region = "UK";
|
||||
|
||||
WriteLine(address.Building?.Length);
|
||||
WriteLine(address.Street.Length);
|
||||
Loading…
Add table
Add a link
Reference in a new issue