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

后端 未结 12 1804
无人及你
无人及你 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条回答
  •  萌比男神i
    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 { ... } 
    

提交回复
热议问题