Search code examples
c#.netaccessor

Is there a way to use parameters with accessors in c#


I really like the syntax for accessors in c#, but I need to use some extra values in my setters for a few properties.

Is there any way I can do something similar to this or is it not possible with this type of syntax?

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

(I'm also finding this hard to search for because I don't know if this has an exact name, so if I could also get that piece of information I would massively appreciate it)

Thank you in advance.


Solution

  • 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 achieved via class methods:

    public void AddObject(CustomObjectType obj, ExtraParameters extraParameters) => 
        AccessorStuff.Add(new CustomObject(obj, ExtraParameters));