在 C# 中这样做安全吗?
field = Property = value;
是否保证setter和getter是连续调用的,field
只赋给getter的结果,不一定是value
?编译器会将其优化为仅 value
吗?
最佳答案
使用
private int tada;
public int TADA
{
get
{
Console.WriteLine("GETTER");
return tada;
}
set
{
Console.WriteLine("SETTER");
tada = value;
}
}
和
int s = TADA = 1;
我只将 SETTER 写入输出窗口,所以它似乎没有调用 getter。
You can even assign the same value to multiple variables, like this:
int a, b, c, d;
a = b = c = d = 5;
In this case, a, b, c, and d would all have the value 5. This works because the C# compiler performs the rightmost assignment first; that is, d = 5. That assignment itself returns a value, the value 5. The compiler then assigns that returned value to c. That second assignment also returns a value, and so on, until all the variables have been assigned.
关于c# - 多重赋值(字段=属性=值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17231905/