相关疑难解决方法(0)

可能是Visual Studio 2015中的C#编译器错误

我认为这是一个编译器错误.

使用VS 2015编译时,以下控制台应用程序编译并执行完美:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = MyStruct.Empty;
        }

        public struct MyStruct
        {
            public static readonly MyStruct Empty = new MyStruct();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是现在它变得很奇怪了:这个代码编译,但它会TypeLoadException在执行时抛出.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = MyStruct.Empty;
        }

        public struct MyStruct
        {
            public static readonly MyStruct? Empty = null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你遇到同样的问题吗?如果是这样,我将在Microsoft提出问题.

代码看起来毫无意义,但我用它来提高可读性并实现消歧.

我有不同的重载方法,如

void DoSomething(MyStruct? arg1, string arg2)

void DoSomething(string arg1, string arg2) …

c# compiler-bug roslyn visual-studio-2015 coreclr

28
推荐指数
2
解决办法
1535
查看次数

标签 统计

c# ×1

compiler-bug ×1

coreclr ×1

roslyn ×1

visual-studio-2015 ×1