mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
Update Chapter02.dib
This commit is contained in:
parent
2cfd8dad10
commit
f827a14e78
|
|
@ -326,43 +326,6 @@ for (int i = 0; i < names.Length; i++)
|
||||||
|
|
||||||
#!markdown
|
#!markdown
|
||||||
|
|
||||||
## Making a value type nullable
|
|
||||||
|
|
||||||
#!csharp
|
|
||||||
|
|
||||||
int thisCannotBeNull = 4;
|
|
||||||
// thisCannotBeNull = null; // compile error!
|
|
||||||
|
|
||||||
int? thisCouldBeNull = null;
|
|
||||||
Console.WriteLine(thisCouldBeNull);
|
|
||||||
Console.WriteLine(thisCouldBeNull.GetValueOrDefault());
|
|
||||||
|
|
||||||
thisCouldBeNull = 7;
|
|
||||||
Console.WriteLine(thisCouldBeNull);
|
|
||||||
Console.WriteLine(thisCouldBeNull.GetValueOrDefault());
|
|
||||||
|
|
||||||
#!markdown
|
|
||||||
|
|
||||||
## Checking for null
|
|
||||||
|
|
||||||
#!csharp
|
|
||||||
|
|
||||||
string authorName = null;
|
|
||||||
|
|
||||||
// the following throws a NullReferenceException
|
|
||||||
// int x = authorName.Length;
|
|
||||||
|
|
||||||
// instead of throwing an exception, null is assigned to y
|
|
||||||
int? y = authorName?.Length;
|
|
||||||
|
|
||||||
Console.WriteLine($"y is null: {y is null}");
|
|
||||||
|
|
||||||
// result will be 3 if authorName?.Length is null
|
|
||||||
var result = authorName?.Length ?? 3;
|
|
||||||
Console.WriteLine(result);
|
|
||||||
|
|
||||||
#!markdown
|
|
||||||
|
|
||||||
## Formatting using numbered positional arguments
|
## Formatting using numbered positional arguments
|
||||||
|
|
||||||
#!csharp
|
#!csharp
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue