gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:28:17 32 4
gpt4 key购买 nike

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

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com