Chinaunix首页 | 论坛 | 博客
  • 博客访问: 873798
  • 博文数量: 372
  • 博客积分: 10063
  • 博客等级: 中将
  • 技术积分: 4220
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 11:36
文章分类

全部博文(372)

文章存档

2012年(372)

分类: 虚拟化

2012-04-26 20:30:17

/// /// C# 1.0 中定义的产品类型 /// public class Product1 { string name; public string Name { get { return name; } } decimal price; public decimal Price { get { return price; } } public Product1(string name, decimal price) { this.name = name; this.price = price; } public static ArrayList GetSampleProducts() { ArrayList list = new ArrayList(); list.Add(new Product1("West Side Story", 9.99m)); list.Add(new Product1("Assassins", 14.99m)); list.Add(new Product1("Frogs", 13.99m)); list.Add(new Product1("Sweeney Todd", 10.99m)); return list; } public override string ToString() { return string.Format("{0}:{1}", name, price); } } /// /// C# 2.0 中的强类型集合 /// public class Product2 { 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 Product2(string name, decimal price) { Name = name; Price = price; } public static List GetSampleProducts() { List list = new List(); list.Add(new Product2("West Side Story", 9.99m)); list.Add(new Product2("Assassins", 14.99m)); list.Add(new Product2("Frogs", 13.99m)); list.Add(new Product2("Sweeney Todd", 10.99m)); return list; } public override string ToString() { return string.Format("{0}:{1}", name, price); } } /// /// C# 3.0 中自动实现的属性 /// public class Product3 { public string Name { get; private set; } public decimal Price { get; private set; } public Product3(string name, decimal price) { Name = name; Price = price; } Product3() { } public static List GetSampleProduct() { return new List { new Product3{Name="West Side Story",Price=9.99m}, new Product3{Name="Assassins",Price=14.99m}, new Product3{Name="Frogs",Price=13.99m}, new Product3{Name="Sweeney Todd",Price=10.99m} }; } public override string ToString() { return string.Format("{0}:{1}", Name, Price); } } /// /// C# 4.0 命名实参带来了清晰的初始化代码 /// public class Product4 { readonly string name; public string Name { get { return name; } } readonly decimal price; public decimal Price { get { return price; } } public Product4(string name, decimal price) { this.name = name; this.price = price; } public static List GetSampleProduct() { return new List { new Product4{name:"West Side Story",price:9.99m}, new Product4{name:"Assassins",price:14.99m}, new Product4{name:"Frogs",price:13.99m}, new Product4{name:"Sweeney Todd",price:10.99m} }; } public override string ToString() { return string.Format("{0}:{1}", name, price); } }
复制代码
阅读(673) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~