[Evolution in action] C#1.1=>2.0=>3.0 [Defining Type]

本文通过一个产品类实例展示了从 C# 1.1 到 C# 3.0 的语法改进过程,包括集合类型的更新、属性设置器的引入以及对象初始化器的应用。

We'll move on to see how the same effect can be achieved in C#2.0,then C#3.0.

image 

C#1.1 Code

using System.Collections;
    public class Product
    {
        string name;
        public string Name
        {
            get { return name; }
        }
        decimal price;
        public decimal Price
        {
            get { return price; }
        }
        public Product(string name, decimal price)
        {
            this.name = name;
            this.price = price;
        }
        public static ArrayList GetSampleProducts()
        {
            ArrayList list = new ArrayList();
            list.Add(new Product("Company", 9.99m));
            list.Add(new Product("Assassins", 14.99m));
            list.Add(new Product("Frogs", 13.99m));
            list.Add(new Product("Sweeney Todd", 10.99m));
            return list;
        }
        public override string ToString()
        {
            return string.Format("{0}: {1}", name, price);
        }
    }

Let's see what C#2.0 can do to improve matter

using System.Collections.Generic;
    public class Product
    {
        string name;
        public string Name
        {
            get { return name; }
            private set { name = value; }
        }
        decimal price;
        public decimal Price
        {
            get { return price; }
           
private set { price = value; }
        }
        public Product(string name, decimal price)
        {
           
Name = name;
            Price = price;
        }
        public static List<Product> GetSampleProducts()
        {
           List<Product> list = new List<Product>();
            list.Add(new Product("Company", 9.99m));
            list.Add(new Product("Assassins", 14.99m));
            list.Add(new Product("Frogs", 13.99m));
            list.Add(new Product("Sweeney Todd", 10.99m));
            return list;
        }
        public override string ToString()
        {
            return string.Format("{0}: {1}", name, price);
        }
    }

Show how C#3.0 tackles these.

class Product
    {
       
public string Name { get; private set; }
        public decimal Price { get; private set; }

        public Product(string name, decimal price)
        {
            Name = name;
            Price = price;
        }

        Product()
        {
        }

        public static List<Product> GetSampleProducts()
        {
           
return new List<Product>
            {
                new Product { Name="Company", Price = 9.99m },
                new Product { Name="Assassins", Price=14.99m },
                new Product { Name="Frogs", Price=13.99m },
                new Product { Name="Sweeney Todd", Price=10.99m}
            };
        }

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

转载于:https://www.cnblogs.com/RuiLei/archive/2008/04/19/1161767.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值