简体   繁体   中英

C# naming and casing conventions

I'm a VB guy that used to prefix module level variables with "_".

I'm using FXCop, StyleCop and (I think the built in Code Analysis, or maybe that is pointing to FXCop, not sure) and I am trying to adopt the most accepted naming conventions. How would you name the following module level private, property and param fields to make all of these code analysis tools happy and conform to commonly accepted or MS standards? Note that having a param name the same as a private module level field can be confusing and FXCop is incorrectly telling me to prefix the "sourcefile" param field reference with "this."

Is my approach to use lower case for module level privates acceptable and all I really need to do is rename the param to something unatural like "mySourceFile" or "sourceFileIn?" It feels forced. params should be Camel cased. Is my module level variable missed cased?

    public class Restartability
    {
        private readonly string sourceFile;

        public Restartability(string sourceFile)
        {
            this.sourceFile = sourceFile;
        }

        public string SourceFile
        {
            get { return sourceFile; }
        }  

   }

I think that private field variables should be prefixed with a '_'.

This would give you:

private readonly string _sourceFile;

However, it can be argued that no prefix should be necessary (from the book Clean Code ), since you shouldn't have so many variables in a class that it becomes hard to tell the difference between field scoped varialbes, and non-field ones.

Ultimately, the only universally accepted naming convention for C# is the following:

  • Namespace names are PascalCased
  • Type names are PascalCased
  • Public and protected members of types, including methods, properties, and fields, are PascalCased

Additionally, it's generally accepted, albeit not universally, that:

  • Method and constructor parameters are camelCased.

The rest vary wildly enough that it doesn't really matter. As long as you adhere to the above, you're golden.

Starting local and member variables with lowercase is common. Where I work we have chosen to prefix member variables with _ , but that is just a matter of convention.

I would write it like this:

public class Restartability {

  private readonly string _sourceFile;

  public Restartability(string sourceFile) {
    _sourceFile = sourceFile;
  }

  public string SourceFile {
    get { return _sourceFile; }
  }  

}

Or using property shorthand:

public class Restartability {

  public string SourceFile { get; private set; }

  public Restartability(string sourceFile) {
    SourceFile = sourceFile;
  }

}

This doesn't make the backing variable read-only, but the setter is private so only code inside the class itself can set it.

When I have private and public properties that have the exact same name, I name the private one with a _ at the end. I think it's a good practice and we all do it here in my company. We all are .net developers

The most common naming schemes for fields of C# objects are

  • A "m_" prefix: m_sourceFile
  • A "_" prefix: _sourceFile
  • No prefix and a fix of using this. to reference or just plain old sourceFile

If you're starting out in C# and thinking about coding style and convention I really recommend a book called The Elements of C# Style !

I also recommend getting yourself a copy of Resharper and the stylecop for resharper plugin

Coding style is a contentious issue amongst developers and there will no doubt be may differing opinions posted here, but the one rule I feel is more important than any is whatever style you use, be consistent.

A very good developer once said to me, no matter how big the project is, it should all look like it was written by the same developer on the same day.

Modern IDE's should have the capability to highlight class specific variables and therefore _ or m_ or this. should be redundant. See: Visual Studio identical token highlighting for more information on this in VS.

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