C# Field Naming Guidelines?

后端 未结 17 1273
天命终不由人
天命终不由人 2020-12-02 16:37

I am going to be working on a bit of C# code on my own but I want to make sure that I follow the most widely accepted naming conventions in case I want to bring on other dev

相关标签:
17条回答
  • 2020-12-02 17:12

    In our shop, we started our first C# project using Microsoft's suggested guideline for private members, i.e.

    camelCaseFieldName
    

    But we soon ran into confusion between private members and parameters, and switched to

    _camelCaseFieldName
    

    which has worked much better for us.

    A private member usually has a state that persists outside of a method call - the leading underscore tends to remind you of that.

    Also note that using AutoVariable syntax for properties can minimize the need for private backing fields, i.e.

    public int PascalCaseFieldName { get; set;}
    

    For a nice concise set of standards that (mostly) follow the MS guidelines, check out net-naming-conventions-and-programming-standards---best-practices

    0 讨论(0)
  • 2020-12-02 17:13

    Philips Healtcare C# Coding Standard

    MSDN - Eric Gunnerson

    Edit: I use "this" keyword to access non-static members in C# and Java.

    0 讨论(0)
  • 2020-12-02 17:15

    Have a look at ReSharper. It will underline all the places where your names do not confirm to ordinary guidelines, and you can customize it. Plus, of course there's loads and loads of other productivity enhancements.

    0 讨论(0)
  • 2020-12-02 17:17

    We use StyleCop to force consistency throughout our code. StyleCop is used within Microsoft enforce a common set of best practices for layout, readability, maintainability, and documentation of C# source code.

    You can run StyleCop at build time and have it generate warnings for style violations.

    To answer your specific question, private fields should be in camelCase and prefixed with "this".

    0 讨论(0)
  • 2020-12-02 17:18
    private string baseName; 
    private string prefixName; 
    private string suffixName; 
    
    public GameItem(string _baseName, string _prefixName, string _suffixName) 
    { 
        this.baseName = _baseName; 
        this.prefixName = _prefixName; 
        this.suffixName = _suffixName; 
    } 
    
    0 讨论(0)
  • 2020-12-02 17:20

    Short answer: use _privateField, i.e. use leading underscore for private fields.

    Long answer: here goes...

    Long long ago, Microsoft used to suggest using camelCase for fields. See here. Note when that document was created, 10/22/2008. Pretty ancient.

    Recent code base of Microsoft however depicts a different picture.

    1. Take a look at the C# Coding style of .NET Runtime GitHub repository. #3 is the point under discussion. Here is the relevant part

      We use _camelCase for internal and private fields and use readonly where possible.

    2. Also take a look at Coding style of Roslyn repository that specifically says that it follows the conventions of .NET Runtime.
    3. Take yet another look at the .NET Standard contributing page, which also says (at least for now) to follow the same guide as .NET CoreFX, which was a precursor to .NET Runtime.
    4. Prior to consolidation, CoreCLR also suggested following the same guide as CoreFX.
    5. Even WinForms repo speaks of using this same standard.
    6. I think I have said enough. So, to conclude, if you want to follow the guide that Microsoft suggests, I think you know what to do; use leading underscore for private fields like this: _privateField.

    My opinion: I too personally prefer leading underscore for my private fields - makes it very easily distinguishable, without needing the this.

    0 讨论(0)
提交回复
热议问题