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


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

  • 相关阅读:
    SQL语句实例学习汇总
    sql语句一些实用技巧for oracle
    无限级递归生成HTML示例及ListBox,DropDownList等无限树
    另类Sql语句直接导出表数据到Execl
    powerdesigner中sql脚本小写转大写,去双引号
    C#中利用jQuery获取Json值示例,Ajax方式。
    利用jquery解决下拉菜单被Div遮挡问题
    获取Textarea 元素当前的光标位置及document.selection.createRange()资料
    oracle中一些常用函数
    IE6 动态创建 iframe 无法显示的 bug,万恶的IE6
  • 原文地址:https://www.cnblogs.com/RuiLei/p/1161767.html
Copyright © 2020-2023  润新知