简体   繁体   English

为什么我不能在构造函数中设置这个readonly属性?

[英]Why can't I set this readonly property in the constructor?

I want to set the readonly porperty "EmployeeList" in the constructor, but for some reason it is not allowed, as the EmployeeList is readonly. 我想在构造函数中设置readonly porperty“EmployeeList”,但由于某种原因它是不允许的,因为EmployeeList是readonly。 But it works just fine for the other readonly property "Manager". 但它适用于其他只读属性“管理器”。 The only difference being that I wrote the getter for EmployeeList myself, and for Manager it is auto-generated. 唯一的区别是我自己为EmployeeList编写了getter,而对于Manager,它是自动生成的。

I'm not allowed to use a private setter to resolve this. 我不允许使用私有的setter来解决这个问题。

class Project
    {
        public Employee Manager { get; }

        public EmployeeList EmployeeList
        {
            //Creates a copy of the EmployeeList and returns it
            get
            {
                //...
                return listCopy;
            }
        }

        //Initializes class-variables.
        public Project(Employee manager, EmployeeList 
                        employeeList)
        {
            Manager = manager;
            EmployeeList = employeeList; //Error: EmployeeList is read-only
        }
}

Why can't I set EmployeeList and how do i fix this? 为什么我不能设置EmployeeList以及如何解决这个问题?

You can set a read-only automatically-implemented property in a constructor, but in your case you've specified an implementation. 您可以在构造函数中设置只读自动实现的属性 ,但在您的情况下,您已指定了一个实现。 That compiler doesn't know what to do when you try to call a "setter" - it doesn't look through the getter and notice that you're returning the listCopy variable. 当你试图调用“setter”时,编译器不知道该怎么做 - 它没有查看getter并注意到你正在返回listCopy变量。 (That may not even be a field - we can't see the rest of your getter code.) (这可能不是一个字段 - 我们无法看到你的getter代码的其余部分。)

So your options are: 所以你的选择是:

  • Change EmployeeList to an automatically-implemented property EmployeeList更改为自动实现的属性
  • Assign directly to the relevant backing field 直接分配给相关的支持字段

EmployeeList is a property. EmployeeList是一个属性。 It has only get accessor method - so you can't set it anyway. 它仅get访问方法-所以你不能设置也无妨。 On the other hand, Manager is a readonly auto property, which is equals to: 另一方面, Manager是一个只读自动属性,它等于:

class Project
{
        public readonly Employee _Manager;
        public Employee Manager 
        {
            get { return _Manager; }
        }

        // ...

        //Initializes class-variables.
        public Project(Employee manager, EmployeeList 
                        employeeList)
        {
            _Manager = manager;
            // Manager = manager; //Error: Manageris read-only
        }
}

So, when you set Manager , you implicitly set its compiler-defined field ( _Manager in this example ), which is readonly. 因此,当您设置Manager ,您隐式设置其编译器定义的字段( _Manager in this example中为_Manager),这是readonly。 But the property Manager (and EmployeeList ) can't be setted by the constructor - it's a property. 但是属性Manager (和EmployeeList )不能由构造函数设置 - 它是一个属性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM