简体   繁体   English

C#错误消息“无法将属性分配给 - 它是只读的。”

[英]C# error message “Property can not be assigned to — it is read only.”

Read only properties can be assigned in a constructor. 只读属性可以在构造函数中指定。 But when I try to explicitly implement get method, compiler shows an error (Property cannot be assigned to -- it is read only.) Can I implement getter or it's supposed to be without implementation? 但是当我尝试显式实现get方法时,编译器显示错误(Property无法分配给 - 它是只读的。)我可以实现getter,或者它应该没有实现吗?

public class PersonalIncome 
{
    private decimal _anualRate;
    public decimal AnualRate
    {
        get { return _anualRate != 0 ? _anualRate : 40_000;  }
    }

    public PersonalIncome(decimal paymentRate)
    {
        switch (paymentRate)
        {
            case var rate when (rate > 300):
                AnualRate = rate; // **Property can not be assigned to -- it is read only.**
                break;
            default:
              break;
        }
    }
}

You can implement the getter, but then you can only assign values to the backing field directly: 您可以实现getter,但是您只能直接将值分配给支持字段:

_anualRate = rate; 

Once you decide against using the convenience of the auto-property, you have to do everything by yourself. 一旦您决定不使用自动财产的便利性,您必须自己做所有事情。

Your class could be rewritten like this: 您的课程可以像这样重写:

public class PersonalIncome
{
    public decimal AnualRate { get; private set; }

    public PersonalIncome(decimal paymentRate)
    {
        AnualRate = paymentRate > 300 ? paymentRate : 40_000;
    }
}

You refer to a property with a readonly backing-field. 您引用具有readonly后备字段的属性。

That´s exactly what the compiler also generates from C#6 upwards when using an auto-implemented property with a default-value: 当使用具有默认值的自动实现属性时,这正是编译器从C#6向上生成的内容:

int MyProperty { get; } = -1;

This will be translated to the following: 这将被翻译成以下内容:

readonly int _myProperty = -1;
int MyProperty { get { return this._myProperty; } }

Now the compiler replaces every call to your property by the backing-field. 现在编译器用后备字段替换对属性的每次调用。 However this only works for auto-properties that do not have a body defined. 但是,这仅适用于未定义主体的自动属性。 In your case you already have one, which is why the compiler can´t replace that one. 在你的情况下你已经有一个,这就是编译器无法替换那个的原因。 As a property itself is nothing but a get- and a set-method, what you want to do is the following, which is obvious non-sense: 作为一个属性本身只是一个get-and set-set方法,你想要做的是以下,这显然是无意义的:

int get_MyProperty() { return this._MyProperty; }
...
this.get_MyProperty() = 1;

The reason this works for an auto-property is that the compiler knows how to replace the call to the property. 这适用于自动属性的原因是编译器知道如何替换对属性的调用。 However suppose your own getter was more complex: 但是假设你自己的吸气剂更复杂:

get
{
    DoSomething();
    return this._myProperty + 5;
}

Now the compiler can´t replace the call to the property. 现在编译器无法替换对属性的调用。

So the only way to have your own get-implementation tigether with a property which is get-only, is to use the backing-field: 因此,使用后备字段来获得与get-only属性一起使用自己的get-implementation的唯一方法是:

this._myProperty = 1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将indexer的属性分配给,它是只读的。 无法保存设置 - Property of indexer cannot be assigned to , it's read only. Can't save settings 无法将属性或索引器“属性”分配给 -- 它在 C# 中是只读的 - Property or indexer 'property' cannot be assigned to -- it is read only in C# C#NET 3.5:无法将属性或索引器分配给“-”,它是只读的 - C# NET 3.5 : Property or indexer cannot be assigned to “--” it is read only 属性或索引器无法分配给它 在 C# 中是只读的 - property or indexer cannot be assigned to it is read only in C# 无法分配 - 它是只读的 - C# - cannot be assigned to — it is read only - C# 属性或索引器&#39;WebApplication1.Default.Credentials&#39;不能分配给它-只能在c#中读取 - property or indexer 'WebApplication1.Default.Credentials' cannot be assigned to — it is read only in c# 属性或索引器不能分配给“ - ”它只读C#List <Tuple<string, bool> &gt; - Property or indexer cannot be assigned to “--” it is read only C# List<Tuple<string, bool>> C# 构造函数如何为只读属性赋值? - How can C# constructor assign value to read only property? 无法分配属性或索引器 - 它是只读的。C# - Property or indexer cannot be assigned to - it is read only.C# 修改变量“属性或索引器”不能分配给错误,它是只读的 - Error when modifying variable “Property or indexer '' cannot be assigned to — it is read only”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM