相关疑难解决方法(0)

为什么我必须使用非默认构造函数初始化C#结构中的所有字段?

我想试试这段代码:

public struct Direction
{
   private int _azimuth;

   public int Azimuth
   {
     get { return _azimuth; }
     set { _azimuth = value; }
   }       

   public Direction(int azimuth)
   { 
      Azimuth = azimuth
   } 
}
Run Code Online (Sandbox Code Playgroud)

但它在编译时失败,我理解struct需要初始化它的所有字段.但我想了解在CLR\IL引擎盖下发生了什么.为什么它需要在任何其他方法\ property\this等之前的所有字段

谢谢.

c# clr stack struct

12
推荐指数
4
解决办法
6406
查看次数

标签 统计

c# ×1

clr ×1

stack ×1

struct ×1