0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【读书笔记】《C# in Depth》~2

Last updated at Posted at 2017-04-18

你们知道我为何喜欢Python,Ruby这些动态语言么?因为他们去代码之糟粕,仅取其精华。
繁琐的形式被生成器,Lambda表达式,列表推导式等特性所替代。

回到C#,C#1呢可以看作是Java语言的升级版,他们的相似之处十分明显,但是C#还包含一些额外的特性:属性,委托,事件,foreach循环,using语句,显示方法重载,操作符重载,自定义类型等。
当我第一次使用C#1时,就明显比Java要更进一步。:rainbow:

别墨迹了,废话少说点吧,直接上Hello World的电子商务版本:

C#1版本的代码:

Product.cs
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("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));
    return list;
  }

  public override string ToString()
  {
    return string.Format("{0}:{1}", name, price);
  }
}

C#2 C#3 我就略去了哈
直接看C#4的进化后的代码吧:

Product.cs
using System.Collections;
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 ArrayList 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);
  }
}

C#2加入了泛型
C#4允许我们在调用构造函数时指定实参的名称

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?