Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# style: May properties be grouped with their backing fields?

I like to organize simple properties like this:

private int foo;
public int Foo
{
    get { return foo; }
    set
    {
        // validate value
        foo = value;
    }
}

I've been playing around with StyleCop, and it yells at me for placing fields after constructors. Is this style generally accepted, as long as the field is never referenced outside of the property? Note: I realize that there is personal preference involved, but I am wondering if there is a general consensus regarding this issue.

like image 402
Matthew Avatar asked Dec 21 '22 13:12

Matthew


1 Answers

Yes, that seems reasonable to me.

Typically I put all the fields and properties at the top, then the constructors, then the methods - but if you want to put them after the constructors, that seems reasonable too.

like image 88
Jon Skeet Avatar answered Feb 23 '23 15:02

Jon Skeet