c# - C# 自动实现的静态属性是线程安全的吗?

标签 c# static thread-safety properties automatic-properties

我想知道 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/

相关文章:

java - 在 spring bean 中访问 Db 时的线程安全

c# - 如何使用委托(delegate)构建 TransformManyBlock

javascript - C# Regex 性能纯相对 JS

dynamic - 静态分配与动态分配与自动分配

java - 接口(interface)的静态和最终规则

java - 静态变量的线程安全

java - Spock 在 "when" block 中运行 Platform.runLater() 时出现计时问题

c# - 使用 MVC C# 和 MySql 的服务器端分页

c# - [JsonProperty] 在 C# 中有何用途?

ios - 静态 NSDateFormatter 始终为 nil