If I have a property do I need a field as well?

So after the availability of automatic implementation, and initialization of a property, do I even need a field for my property?

This seems much cleaner:

class A {
public int X {
        get;set;
    } = 1;
}

Than this:

class A {
    int x = 1;
    public int X {
        get {
            return x;
        }
        set {
            x = value;
        }
    }
}
Jon Skeet
people
quotationmark

In the first case, the compiler is already providing a backing field - it's just implicit (and it's given a name that you can't refer to in code). Note that there has to be a backing field in the generated code, as a property itself is really just a pair of methods with some metadata linking them - the presence of a property does not add any state to the object. State is only stored in fields.

It's even cleaner when written on one line - I'd usually see this as:

class A
{
    public int X { get; set; } = 1;
}

people

See more on this question at Stackoverflow