简体   繁体   中英

C# .NET instance variable naming convention?

I'm doing a small internship at a business and in their code I find classes that are named like this:

public class FlagsConfig
{
    private static FlagsConfig _instance; 
}

Is the _instance a naming convention of any sort in C#?

I would ask the developers but they are all out today and the next week on some course.

Maybe this can help you: .net Naming Conventions and Programming Standards - Best Practices

According to this document, it is ok.

For private members, there are lots of different conventions. Some people like prefixes, some don't (personally I don't). Some like to differentiate between instance variables and static variables, others don't:

private string m_foo;
private static string s_foo;

Personally I find the underscores get in the way when I'm reading the text - and I firmly believe it depends on how you read; I subvocalize when I read, and the extra bits get in the way of that. For others, it's clearly not a problem. Others find the lack of distinction between local variables and member variables a problem - I typically write short methods where it's obvious what's what anyway.

What's more important - certainly if you're creating an API etc is the naming of publicly visible members (which includes protected ones, and parameter names) at which point you should look at the Microsoft guidelines .

Is the _instance a naming convention of any sort in C#?

First off, a number of people have referenced the naming guidelines. Note that many of those guidelines apply only to the public surface area of a type. Private members like the one you mention are internal implementation details and therefore subject to the policies of the organization that produced them, not subject to the framework design guidelines for what people expect to see in a public element.

For private implementation details the underbar prefix is common in many organizations. I personally don't think it is necessary, but some people seem to like it.

What is important however is that even for private implementation details you should never use two underbars. The C# compiler team reserves the right to make any word that begins with two underbars to have any meaning we choose in some future version of the language. That is our "escape hatch" in case we really, really need to add a new non-contextual reserved keyword and really, really do not want to break any existing code.

This is documented in section 2.4.2 of the C# 4 specification.

Yes, that is a common naming standard for private fields:

http://csharpguidelines.codeplex.com/

I happen to agree with @JonSkeet that the underscores are messy, but AFAIK that is the MS standard. The document he links to indicates not using underscores in your library, but I believe that is referring to public members.

Update

The first link actually advocates the opposite; don't use underscores. My mistake, but it's still a useful resource.

In deference to Mr. Skeet, I followed his link further to: http://msdn.microsoft.com/en-us/library/ms229012.aspx which also states that you shouldn't use underscores, but that guidance applies to static, protected and public members, but not necessarily to private members.

Bottom Line : Yes it is a common standard, but first use any internally agreed upon standard before trying to find/use external standards.

There are many guidelines and standards to choose from, but if the standard used at your workplace uses underscores, then that is what you need to use. Especially if you are only doing an internship there, the goal should be to keep things consistent (within that business) rather than following some standard which is "better" (but different).

Perhaps the better question to ask your developers (or the higher up bosses) is if they have any documentation/links on the standards that they do use?

That is relatively common in my experience. To help identify particular kinds of variables (privates, method parameters etc.), a developer may employ different naming conditions.

eg

  • VariableName
  • variableName (camel case)
  • _variable
  • VARIABLE_NAME

It tends to vary by company I think.

_name is messy, confusing and very old-style. don't do it.

.NET 4.0 General Naming Conventions http://msdn.microsoft.com/en-us/library/ms229045.aspx

as you can see, MSDN states

Do not use underscores, hyphens, or any other nonalphanumeric characters

I like to use a case change to distinguish between fields and properties:

// A private field
private Boolean someValue;
// A public property, exposing my private field
public Boolean SomeValue {
    get { return someValue; }
    set { someValue = value; }
}

Are your co-workers ex-VB devs? In VB.Net the underscore is used regularly for private members of properties or classes. Since VB is case insensitive, you can't use case to distinguish.

Private _someValue As Boolean
Protected Property SomeValue() As Boolean
    Get
        Return _someValue
    End Get
    Set(ByVal value As Boolean)
        _someValue = value
    End Set
End Property

Update: As an aside, many classes in the .NET source code use this convention. Especially in System.Web .

There are two common conventions.

the first is "User underscore as field marker" the second is "Use s_ for static fields and m_ for intance fields"

imo this is a religious question and onnly important thing is to not mix up both styles.

This book contains many good ideas about convention and design guidelines

http://www.amazon.de/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=sr_1_1?ie=UTF8&qid=1320395003&sr=8-1

According to StyleCop [A style/convention checking tool by Microsoft) it shouldn't be done. See: http://stylecop.soyuz5.com/SA1309.html

Also, question is a possible dupe of To underscore or to not to underscore, that is the question

There are many naming conventions that people follow

myFirstVar = Camel Notation

Camel notaion is generally used for public variables (not private variables).

MyFirstVar = Pascal Notation

Pascal is generally used for naming Classes and Methods.

str_MyFirstVar = Hungarian Notation // if variable is of type string

Hungarian Notation is considered to be the oldest but not used anymore.

_myFirstVariable = used for private fields in general

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM