Should a property have the same name as its type?

后端 未结 9 949
醉梦人生
醉梦人生 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.

提交回复
热议问题