C# and Game(4)Nullable

本文介绍了 C# 中 Nullable 类型的基本概念及其使用方法。Nullable 类型允许整数和其他值类型拥有 null 值,这对于处理可能不存在的数据非常有用。文章通过示例展示了 Nullable 类型的定义与使用,并说明了如何安全地从 Nullable 类型转换到基本类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值