Should a property have the same name as its type?

后端 未结 9 901
醉梦人生
醉梦人生 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:25

    This common pattern is one of the reasons why I always use this when referring to an instance member within a class. e.g. always

    this.SomeMethod(this.SomeProperty);
    

    and never

    SomeMethod(SomeProperty);
    

    In most cases, there isn't any actual ambiguity, but I find it helps clarify things. Plus you now know where the property/method is defined.

    0 讨论(0)
  • 2020-12-01 06:27

    I can only think of one drawback. If you wanted to do something like this:

    public class B1
    {
            public static void MyFunc(){ ; }
    }
    
    public class B2
    {
            private B1 b1;
    
            public B1 B1
            {
                    get { return b1; }
                    set { b1 = value; }
            }
    
            public void Foo(){
                    B1.MyFunc();
            }
    }
    

    You'd have to instead use:

    MyNamespace.B1.MyFunc();
    

    A good example of this is common usage is in Winforms programming, where the System.Windows.Forms.Cursor class overlaps with the System.Windows.Forms.Form.Cursor property, so your form events have to access static members using the full namespace.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-01 06:31

    It can obviously be a bit confusing when the name of a property and it's type are the same, but other than that it's not really a problem.

    If the name makes sense, it's usually better to let the name and the type be the same. If you can think of a better name, you should of course use that, but you should not try to make up a name at any cost just to avoid this situation.

    0 讨论(0)
  • 2020-12-01 06:34

    There's no specific technical problem with it. It might harm or improve readability. In fact, some Microsoft libraries have these kind of properties (specifically, with enum properties, this usually makes sense).

    0 讨论(0)
  • 2020-12-01 06:43

    Just today, Eric blogged about the 'Color Color' problem.

    http://blogs.msdn.com/ericlippert/archive/2009/07/06/color-color.aspx

    Personally, I would avoid it if possible.

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