Search code examples
c#properties

C# properties: how to use custom set property without private field?


I want to do this:

public Name
{
    get;
    set
    {
        dosomething();
        ??? = value
    }
}

Is it possible to use the auto-generated private field?
Or is it required that I implement it this way:

private string name;
public string Name
{
    get
    {
        return name;
    }
    set
    {
        dosomething();
        name = value
    }
}

Solution

  • Once you want to do anything custom in either the getter or the setter you cannot use auto properties anymore.