VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C/C++语言编程 >
  • C#教程之c#中使用自动属性减少代码输入量

 
代码如下:

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;
        }
    }

 


可以改写为:

 

 

复制代码 代码如下:

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);
        }
    }


相关教程