C# 1.0 到 4.0 的进化 1

定义一个产品类 Product

C# 1

using System;
using System.Collections;

namespace C1
{

    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("West Side Story", 9.99m));
            list.Add(new Product("Assassins", 14.99m));
            list.Add(new Product("Frogs", 13.99m));
            list.Add(new Product("Sweeney Todd", 10.99m));
            //list.Add("可以插入一个字符串,在编译期间不出错!");
            return list;
        }
        public override string ToString()
        {
            return String.Format("{0}: {1}", name, price);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

 C# 2

using System;
using System.Collections.Generic;


namespace C2
{
    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("West Side Story", 9.99m));
            list.Add(new Product("Assassins", 14.99m));
            list.Add(new Product("Frogs", 13.99m));
            list.Add(new Product("Sweeney Todd", 10.99m));
            //list.Add("错误!");
            return list;
        }
        public override string ToString()
        {
            return String.Format("{0}: {1}", name, price);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

 C# 3

using System;
using System.Collections.Generic;

namespace C3
{
    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 = "West Side Story", Price = 9.99m },
                new Product{ Name = "Assassins", Price = 14.99m },
                new Product{ Name = "Sweenty Todd", Price = 10.99m }
            };
        }
        public override string ToString()
        {
            return String.Format("{0}: {1}", Name, Price);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

 C# 4

using System;
using System.Collections.Generic;


namespace C4
{
    public class Product
    {
        readonly string name;
        public string Name { get { return name; } }
        readonly decimal price;
        public decimal Price { get { return price; } }
        public Product(string name, decimal price)
        {
            this.name = name;
            this.price = price;
        }
        public static List<Product> GetSampleProducts()
        {
            return new List<Product>
            {
                new Product(name:"West Side Story", 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);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

 

posted @ 2014-06-05 22:59  海阔天空XM  阅读(350)  评论(0编辑  收藏  举报