Should a property have the same name as its type?

后端 未结 9 947
醉梦人生
醉梦人生 2020-12-01 06:06

I\'ve sometimes seen code written like this :

public class B1
{
}

public class B2
{
    private B1 b1;

    public B1 B1
    {
        get { return b1; }
           


        
9条回答
  •  心在旅途
    2020-12-01 06:29

    I give things the same name as their type, except for case: my methods and properties are "lowerCase"; and I therefore wouldn't have the problem that MiffTheFox has.

    public class B1
    {
        public static void myFunc(){ ; }
    }
    
    public class B2
    {
        private B1 m_b1;
    
        public B1 b1
        {
            get { return m_b1; }
            set { m_b1 = value; }
        }
    
        public void Foo()
        {
            B1.myFunc(); //this is Ok, no need to use namespace
        }
    }
    

    So for me, m_b1 is member data, b1 is a property (or a local variable or parameter), and B1 is the name of the class.

提交回复
热议问题