C# and Game(4)Nullable

C# and Game(4)Nullable

Nullable Types
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/nullable-types/

Nullable types are instances of the System.Nullable<T> struct.
Nullable<bool> means true, false, null.

The syntax T? is shorthand for Nullable<T>
int? i = 10;
double? d1 = 3.14;

Sample Code:
int? x = 10;
if(x.HasValue){
    System.Console.WriteLine(x.Value);
}else{
    System.Console.WriteLine(“Undefined");
}

The ?? Operator
int? c = null;
//d = c, unless c is null, in which case d = -1. -1 is likely the default value if c is null
int d = c ?? -1;

Identify a Nullable Type
int? i = 5;
Type t = i.GetType();
Console.WriteLine(t.FullName); // System.Int32

Safely Cast from boo? to bool
bool? test = null;
if(!test.HasValue){
    //test is null
    test = true;
}
if((bool)test){}   //now this cast is safe

There a more examples and docs here.
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/index


Weekly Summary
Done:
1 Walk through basic document C# programming guide, follow the examples and run most of the codes in Vision Studio
TODO:
1 Try to understand the IDE unity and project structure
2 Two options, #1 follow the tutorials on official website
                        #2 a complete project “warehouse” or similar game


References:
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/index

https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326364484&siteId=291194637