Dmi*_*gin 16 c# struct nullable
创建一个控制台应用程序来重现:
struct Test
{
public static readonly Test? Null = null;
}
class Program
{
static void Main(string[] args)
{
var t = Test.Null;
}
}
Run Code Online (Sandbox Code Playgroud)
它是可编译的,但我们将在运行时具有以下内容:
mscorlib.dll中发生了未处理的"System.TypeLoadException"类型异常.附加信息:无法从程序集"ConsoleApplication17,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null"加载类型"ConsoleApplication17.Test".
这种方法解决了这个问题:
struct Test
{
public static Test? Null => null;
}
Run Code Online (Sandbox Code Playgroud)