none
What is the c# protected property naming convention? RRS feed

  • Question

  • Dear All,


    What is the c# protected property naming convention?

    For instance, if there is a protected property string,

    naming convention will be

    _name, name, Name or others?


    Thanks and Best regards,

    E-John

        public class BaseClass
        {
            // protected property naming convention?
            // _name, name or Name?
            protected string name;
    
            public BaseClass()
            {
                name = "Base Class";
            }
        }
    
        public class DerivedClass : BaseClass
        {
            public DerivedClass()
            {
                name = "Derived Class";
            }
        }


    • Edited by E-John Friday, March 29, 2019 9:24 AM
    Friday, March 29, 2019 8:24 AM

Answers

  • Hello,

    The standard would be uppercased

    public class BaseClass
    {
        // protected property naming convention?
        // _name, name or Name?
        protected string Name;
    
        public BaseClass()
        {
            Name = "Base Class";
        }
    }
    
    public class DerivedClass : BaseClass
    {
        public DerivedClass()
        {
            Name = "Derived Class";
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by E-John Friday, March 29, 2019 2:08 PM
    Friday, March 29, 2019 12:01 PM
    Moderator
  • Expanding on Karen's answer. Pascal casing is used for members that are used outside the type. Underscores and camel casing  are used for fields (which are almost always private). Private members are the only ones that seem to have any inconsistency as some people use camel casing (no underscore) while others use Pascal. It is private so nobody really cares.

    Protected is still exposing "public" members. The difference is that those "public" members are only accessible to derived types. So protected follows the same rules as public. Internal would as well since an internal member is still "public", just to the assembly.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by E-John Friday, March 29, 2019 2:08 PM
    Friday, March 29, 2019 1:51 PM
    Moderator

All replies

  • Hello,

    The standard would be uppercased

    public class BaseClass
    {
        // protected property naming convention?
        // _name, name or Name?
        protected string Name;
    
        public BaseClass()
        {
            Name = "Base Class";
        }
    }
    
    public class DerivedClass : BaseClass
    {
        public DerivedClass()
        {
            Name = "Derived Class";
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by E-John Friday, March 29, 2019 2:08 PM
    Friday, March 29, 2019 12:01 PM
    Moderator
  • Expanding on Karen's answer. Pascal casing is used for members that are used outside the type. Underscores and camel casing  are used for fields (which are almost always private). Private members are the only ones that seem to have any inconsistency as some people use camel casing (no underscore) while others use Pascal. It is private so nobody really cares.

    Protected is still exposing "public" members. The difference is that those "public" members are only accessible to derived types. So protected follows the same rules as public. Internal would as well since an internal member is still "public", just to the assembly.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by E-John Friday, March 29, 2019 2:08 PM
    Friday, March 29, 2019 1:51 PM
    Moderator
  • Hi CoolDadTx,

    Thanks for your explanations in detail.

    Best regards,

    E-John

    Friday, March 29, 2019 2:09 PM
  • Hi Karen,

    Understood, thanks for your helps.

    Best regards,

    E-John

    Friday, March 29, 2019 2:10 PM