C#8.0可空引用类型
链接:https://docs.microsoft.com/zh-cn/dotnet/csharp/tutorials/nullable-reference-types
运行环境:.NET Core3.1
public static int? a; public static int aa; public static string str; public static string? str1; public static string? Get() { return null; } static void Main(string[] args) { Console.WriteLine(a); Console.WriteLine(aa); Console.WriteLine(str); Console.WriteLine(str1); Console.WriteLine(str==str1);//true 比较的是这两个字段的值 //Console.WriteLine(str.Equals(str1)); //这一行会报空值导致的错误 Console.WriteLine("----------"); Get(); }
当Get方法的返回值为可空类型时依旧可以跑起来,在Unity.NET 4.x 环境下直接会报错 导致代码不能编译通过
虽然写了可空 但是编辑器会在问号下面加上一个波浪线提示需要……,这个时候需要,双击程序集,会进入一个xml文件,添加如下高亮代码就不会出现波浪线了。


浙公网安备 33010602011771号