• 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)
            {
            }
        }
    }
    

     

  • 相关阅读:
    Eclipse部署项目到Tomcat中,class文件夹为空的解决方案
    微软(北京).NET俱乐部活动 (2010年6月26日) – Visual Studio 2010 /*LIFE RUNS ON CODE*/
    失望的Vista SP1
    急聘BI DW OLAP开发工程师 (北京)
    急聘.NET开发工程师 (北京)
    开篇
    Windows Vista User Account Control (UAC) 全新安全模块“用户帐户控制”
    Tidy your desktop
    [导入]Vista的屏幕截图小工具:Snipping Tool
    微软发布官方TFS 2010 Scrum 模板
  • 原文地址:https://www.cnblogs.com/wanghaibin/p/3771325.html
Copyright © 2020-2023  润新知