zoukankan      html  css  js  c++  java
  • [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);
            }
        }

  • 相关阅读:
    HarmonyOS Java UI之DirectionalLayout布局
    【HarmonyOS HiSpark IPC DIY Camera】hi3518wifi的配置与使用
    《鸿蒙开发板外设控制》直播答疑(初学者必看)
    【鸿蒙应用开发】确切位置布局PositionLayout
    鸿蒙应用开发之怎么更好的远程连接手表模拟器做调试
    [Hi3861]实现S1,S2,User三个物理按键的独立事件下(DTButtonV0.0.3)
    鸿蒙开发板外设控制 之 实现物理按键的“长按事件”(按键通用框架 V0.0.2)
    单一方向布局实现音乐播放UI
    鸿蒙系统应用开发之JS实现一个简单的List
    动态设置cxgrid列的Properties
  • 原文地址:https://www.cnblogs.com/RuiLei/p/1161767.html
Copyright © 2011-2022 走看看