我想知道 C# 是否自动实现了属性,例如 public static T Prop { get;放; }
,是否线程安全。谢谢!
最佳答案
C# 规范的第 10.7.4 节指出:
When a property is specified as an automatically implemented property, a hidden backing field is automatically available for the property, and the accessors are implemented to read from and write to that backing field. The following example:
public class Point {
public int X { get; set; } // automatically implemented
public int Y { get; set; } // automatically implemented
}
is equivalent to the following declaration:
public class Point {
private int x;
private int y;
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
}
这是我们的 promise ,也是您得到的。 auto properties 的意义在于做最基本、最简单、最廉价的事情;如果你想做一些更奇特的事情,那么你应该写一个“真正的”属性。
关于c# - C# 自动实现的静态属性是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2074670/