Is there any C# naming convention for a variable used in a property?

后端 未结 12 1671
无人及你
无人及你 2021-01-31 07:26

Let\'s say, we have a variable, which we want named Fubar

Let\'s say that Fubar is a String!

That means, we would define F

相关标签:
12条回答
  • 2021-01-31 08:18

    Unluckily there are no common convention, you have to choose what suits most your case, I've seen all the following approaches in different codebases.

    Approach 1

    private string _fubar;   //_camelCase
    public string Fubar { ... }
    

    Approach 2

    private string fubar;    //camelCase
    public string Fubar{ ... }
    

    Approach 3

    private string _Fubar;    //_PascalCase
    public string Fubar{ ... }
    

    Also there are frameworks that takes much creativity like using a property and document it as a member variable and thus using member's styling instead of the properties' styling ( yeah Unity! I'm pointing the finger at you and your MonoBehaviour.transform 's property/member)

    To disambiguate in our code base we use our homemade rule:

    • Try to use more proper naming, usually a member used inside a public property has a slightly different purpose than its public counterpart, so it is very possible most times to find a different and proper name, and if not possible its purpose is just holding state for the public property, so why not naming it nameValue?
    • use autoproperties if possible

    With our approach most times we avoid the doubt about the underscore "_" while at same time having a much more readable code.

    private string fubarValue; //different name. Make sense 99% of times
    public string Fubar { ... } 
    
    0 讨论(0)
  • 2021-01-31 08:21

    Another way to declare with a default value

        private string _fubar = "Default Value";
        public string Fubar
        {
            get { return _fubar; }
            set { _fubar = value; }
        }
    
    0 讨论(0)
  • 2021-01-31 08:25

    I thing, one name is better:

    public string Fubar { get; private set; }
    
    0 讨论(0)
  • 2021-01-31 08:26

    While most developers follow Microsoft's guideline, as game developers, we follow Unity's style as (one of the script source code here):

    static protected Material s_DefaultText = null;
    protected bool m_DisableFontTextureRebuiltCallback = false;
    public TextGenerator cachedTextGenerator { ... }
    
    0 讨论(0)
  • 2021-01-31 08:27

    I see a ton of outdated answers (and non-standard ways representing C#6), so this one's for 2020:

    // "fubar" works, too, but "_" prevents CaSe typo mistakes
    private string _fubar;
    public string Fubar
    {
        get => _fubar;
        set => _fubar = value;
    }
    
    // Read-only can just use lambda to skip all those shenannigans
    public string ReadOnlyFoo => "This is read-only!";
    
    0 讨论(0)
  • 2021-01-31 08:28

    If there's no logic in the getter/setter, use an auto-property:

    public string Fubar {get; set;}
    

    http://msdn.microsoft.com/en-us/library/bb384054.aspx

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