有没有一种方法可以在C#中对访问器使用参数

Rya*_*han 1 .net c# accessor

我真的很喜欢c#中访问器的语法,但是我需要在setter中使用一些额外的值来获取一些属性。

有什么办法可以做类似的事情,或者使用这种语法是不可能的吗?

public class Accesssor{
    public List<CustomObject> AccessorStuff { 
        get {
                return AccessorStuff;
            } 
        set (ExtraParameters????) => {
                AccessorStuff.add(new CustomObject(value,ExtraParameters))
            }
        }
}

Run Code Online (Sandbox Code Playgroud)

(我也很难找到它,因为我不知道它的名字是否正确,因此,如果我也能获得那条信息,我将不胜感激)

先感谢您。

Tom*_*mer 6

No, this is not possible - and not what I'd expect if I used your property. When I use set I expect to change the List, not add values to it. This is most commonly acheived via class methods:

public void AddObject(CustomObjectType obj, ExtraParameters extraParameters) => 
    AccessorStuff.Add(new CustomObject(obj,ExtraParameters));
Run Code Online (Sandbox Code Playgroud)