c# - 多重赋值(字段=属性=值)

标签 c# .net properties variable-assignment assignment-operator

在 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。

来自 C# Language Fundamentals

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/

相关文章:

c# - 由于一个或多个外键属性不可为空,因此无法更改关系。 (2)

c# - 有没有人有从 .Net 应用程序控制多个 Excel 实例的好例子?

c# - 如何将 WebService 添加到 C# WinForm?

c# - 仅使用新记录进行更新时,DataTable.Load 方法不起作用

c# - 如何在 Xamarin 表单中将两个以上的列表合并在一起?我现在使用 concat 但这只允许我将两个列表合二为一

c# - 如何通过 PayPal IPN 传递自定义数据

Spring属性占位符配置正整数

c++ - 从 C++ 访问 ListElement (QML) 颜色

用于包装外部库的 Python 属性工厂或描述符类

c# - 如何在 Visual Studio 2015 中安装 ag-grid?