Naming convention - underscore in C++ and C# variables

前端 未结 19 2370
春和景丽
春和景丽 2020-11-28 01:11

It\'s common to see a _var variable name in a class field. What does the underscore mean? Is there a reference for all these special naming conventions?

19条回答
  •  天命终不由人
    2020-11-28 01:54

    The underscore is simply a convention; nothing more. As such, its use is always somewhat different to each person. Here's how I understand them for the two languages in question:

    In C++, an underscore usually indicates a private member variable.

    In C#, I usually see it used only when defining the underlying private member variable for a public property. Other private member variables would not have an underscore. This usage has largely gone to the wayside with the advent of automatic properties though.

    Before:

    private string _name;
    public string Name
    {
        get { return this._name; }
        set { this._name = value; }
    }
    

    After:

    public string Name { get; set; }
    

提交回复
热议问题