Should a property have the same name as its type?

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

    It's fine. The canonical example here is

    public Background {
        public Color Color { get; set; }
    }
    

    There are rare issues (corner cases) that come up here, but not enough to warrant avoiding this device. Frankly, I find this device quite useful. I would not enjoy not being able to do the following:

    class Ticker { ... }
    
    
    public StockQuote {
        public Ticker Ticker { get; set; }
    }
    

    I don't want to have to say Ticker StockTicker or Ticker ThisTicker etc.

提交回复
热议问题