1.1.1 从简单的数据类型开始


    /// <summary>
    /// C# 1.0 中定义的产品类型
    /// </summary>
    public class Product1
    {
        string name;
        public string Name { get { return name; } }

        decimal price;
        public decimal Price { get { return price; } }

        public Product1(string name, decimal price)
        {
            this.name = name;
            this.price = price;
        }

        public static ArrayList GetSampleProducts()
        {
            ArrayList list = new ArrayList();
            list.Add(new Product1("West Side Story", 9.99m));
            list.Add(new Product1("Assassins", 14.99m));
            list.Add(new Product1("Frogs", 13.99m));
            list.Add(new Product1("Sweeney Todd", 10.99m));
            return list;
        }

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

    /// <summary>
    /// C# 2.0 中的强类型集合
    /// </summary>
    public class Product2
    {
        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 Product2(string name, decimal price)
        {
            Name = name;
            Price = price;
        }

        public static List<Product2> GetSampleProducts()
        {
            List<Product2> list = new List<Product2>();
            list.Add(new Product2("West Side Story", 9.99m));
            list.Add(new Product2("Assassins", 14.99m));
            list.Add(new Product2("Frogs", 13.99m));
            list.Add(new Product2("Sweeney Todd", 10.99m));
            return list;
        }

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

    /// <summary>
    /// C# 3.0 中自动实现的属性
    /// </summary>
    public class Product3
    {
        public string Name { get; private set; }
        public decimal Price { get; private set; }

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

        Product3() { }

        public static List<Product3> GetSampleProduct()
        {
            return new List<Product3>
            {
                new Product3{Name="West Side Story",Price=9.99m},
                new Product3{Name="Assassins",Price=14.99m},
                new Product3{Name="Frogs",Price=13.99m},
                new Product3{Name="Sweeney Todd",Price=10.99m}
            };
        }

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

    /// <summary>
    /// C# 4.0 命名实参带来了清晰的初始化代码
    /// </summary>
    public class Product4
    {
        readonly string name;
        public string Name { get { return name; } }

        readonly decimal price;
        public decimal Price { get { return price; } }

        public Product4(string name, decimal price)
        {
            this.name = name;
            this.price = price;
        }

        public static List<Product4> GetSampleProduct()
        {
            return new List<Product4>
            {
                new Product4{name:"West Side Story",price:9.99m},
                new Product4{name:"Assassins",price:14.99m},
                new Product4{name:"Frogs",price:13.99m},
                new Product4{name:"Sweeney Todd",price:10.99m}
            };
        }

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

优质内容筛选与推荐>>
1、Jquery 提示插件alertify 【备用】
2、spring04-transaction
3、远程桌面不能粘贴复制
4、iOS播放音乐
5、初识Python,望君多多关照


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号