Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you group private fields or put them with their property?

I've seen (and used) on various projects this layout, with a group of fields followed by a group of properties:

private int MyIntField;
private string MyStringField;

public int MyInt { 
    get { return MyIntField; }
    set { MyIntField = value; }
}    
public string MyString { 
    get { return MyStringField; }
    set { MyStringField = value; }
}

And I've also encountered this layout with fields next to their property:

private int MyIntField;
public int MyInt { 
    get { return MyIntField; }
    set { MyIntField = value; }
}    

private string MyStringField;
public string MyString { 
    get { return MyStringField; }
    set { MyStringField = value; }
}

Is there a reason to consider one better than the other? I think most coding standards recommend Option #1, but sometimes it's handy having the field next to the property that operates on it.

Note: I'm assuming non-trivial properties that can't use auto-implemented properties.

like image 510
Stewart Johnson Avatar asked Nov 08 '08 14:11

Stewart Johnson


People also ask

Should I use fields or properties?

You should always use properties where possible. They abstract direct access to the field (which is created for you if you don't create one). Even if the property does nothing other than setting a value, it can protect you later on.

What is difference between field and property?

Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.

Why is it better to make C# fields private?

Generally, you should use fields only for variables that have private or protected accessibility. Data that your type exposes to client code should be provided through methods, properties, and indexers. By using these constructs for indirect access to internal fields, you can guard against invalid input values.

Can properties be private?

Properties can be marked as public , private , protected , internal , protected internal , or private protected . These access modifiers define how users of the class can access the property.


6 Answers

I think it is whatever the team feels comfortable with. Settle on a standard for the project/company/language and stick to it. I prefer the private variables all together, the methods/interfaces together, the private members....I think you get the point.

like image 81
kenny Avatar answered Nov 15 '22 00:11

kenny


I group them at the top of the class.

In fact the only thing that is over my private attribute is all constant of the class.

like image 45
Patrick Desjardins Avatar answered Nov 14 '22 23:11

Patrick Desjardins


I kinda like to have the fields grouped at the top and the properties somewhere else. This is also what Microsoft StyleCop recommends.

like image 35
hangy Avatar answered Nov 14 '22 23:11

hangy


I'd do the latter approach, since it's a convention that helps me to see at a glance whether a private member has a public getter/setter or not. Not a huge deal either way, though.

like image 41
MusiGenesis Avatar answered Nov 15 '22 00:11

MusiGenesis


To reiterate what Kenny said above, it's really all about the coding standards of your organization. It's hard to objectively classify one style over the other, although everyone seems to have their own opinion.

I generally tend to prefer having data and methods groups by access modifier, and so in this case would prefer Option #1. This is to emphasize the interface, not the design. That is, I could transparently change the implementation of the MyInt modifier in the future (maybe I don't really need to store a backing variable).

like image 30
Scott Wegner Avatar answered Nov 15 '22 00:11

Scott Wegner


As a side note where do auto properties fit in?

C# 3.0 auto-properties - useful or not?

like image 37
JaredCacurak Avatar answered Nov 14 '22 23:11

JaredCacurak