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

來源:互聯網
上載者:User

We'll move on to see how the same effect can be achieved in C#2.0,then C#3.0.

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.