亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c#中使用自動屬性減少代碼輸入量

 更新時間:2012年12月04日 15:28:36   作者:  
.Net 3.0中的自動屬性可以大幅度降低我們輸入的代碼量,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

public class Product
    {
        private String name;
        public String Name
        {
            get
            {
                return name;
            }
            private set
            {
                name = value;
            }
        }

        private Decimal price;
        public Decimal Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }

        public Product(String name, Decimal price)
        {
            this.price = price;
            this.name = name;
        }
    }


可以改寫為:

復(fù)制代碼 代碼如下:

public class Product
    {
        public String Name
        {
            get;
            private set;
        }

        public Decimal Price
        {
            get;
            set;
        }

        public Product(String name, Decimal price)
        {
            Name = name;
            Price = price;
        }

        public override string ToString()
        {
            return String.Format("{0}:{1}", this.Name, this.Price);
        }
    }



代碼是不是簡化了很多!

注意:

不能定義只讀或者只寫的屬性,必須同時提供
如果想在屬性中增加判斷、驗(yàn)證等邏輯,則只能用傳統(tǒng)的屬性定義方法實(shí)現(xiàn)

 

相關(guān)文章

最新評論