Welcome Guest Search | Active Topics | Members | Log In | Register

Naming conventions private fields C# Options · View
ebovende
Posted: Thursday, July 03, 2008 10:30:25 PM

Rank: Administration
Groups: Administration

Joined: 5/29/2008
Posts: 2
Points: 6
Unfortunately, that will only work when using both get and set initializers. When using only a set or a get initializer, you still have to create your own field. Besides that, when using this method, you can also use public fields.
rboeije
Posted: Monday, July 21, 2008 2:32:50 PM

Rank: Advanced Member
Groups: Member

Joined: 6/13/2008
Posts: 31
Points: 93
Location: Maarssen
A reason to use underscore would be that you don't accidentally use the field instead of the property within the same class.
rwolff
Posted: Sunday, October 19, 2008 10:37:05 AM

Rank: Administration
Groups: Administration

Joined: 5/29/2008
Posts: 14
Points: 42
Location: Zoetermeer
What are the naming convention for private fields in c#.

Especially the discussion if a private fields must have a prefix like a underscore. The Design Guidelines from Microsoft are not clear in this. In the examples of Microsoft you find with and without underscores...

Why use underscore?
- Never the same variables as parameter and field:

Code:
private void Test(string text)
{
  this.text = text;

  //or

  _text = text
}


why not use underscore?
- When selecting a code line you sometimes don't see the underscore.

What do you thing?



Robert de Wolff - ADP Core Team
ebovende
Posted: Wednesday, October 22, 2008 11:25:37 PM

Rank: Administration
Groups: Administration

Joined: 5/29/2008
Posts: 2
Points: 6
You should also keep CLS Compliancy in mind. When using the same name for a field and a property, you are not CLS Compliant (case doens't matter).

Personally, I always use an underscore to prefix my private fields.
bartreyserhove
Posted: Thursday, October 23, 2008 8:56:17 PM
Rank: Newbie
Groups: Member

Joined: 6/3/2008
Posts: 7
Points: 21
Location: Belgium
rboeije
Posted: Thursday, May 07, 2009 3:40:03 PM

Rank: Advanced Member
Groups: Member

Joined: 6/13/2008
Posts: 31
Points: 93
Location: Maarssen
Latest version of Resharper (4.5) suggests you to use underscores for private and protected fields.
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

Theme Created for the Accelerated Delivery Platform
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.
This page was generated in 0.054 seconds.