All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
关于PHP动态网页session问题的解决方案_php资料_编程技术
Apache下服务器虚拟主机的设置方法介绍_php资料_编程技术
PHP学习宝典-第二章 (续篇)_[PHP教程]
数据库的一些常用知识_[SQL Server教程]
对.NET,Framework,反射的反思_.net资料_编程技术
在DataList中使用用户控件_[Asp.Net教程]
ASP.NET,2.0打造购物车和支付系统之一_[Asp.Net教程]
Asp.net连接数据实例(ACCESS版)_[Asp.Net教程]
ADO.NET删除数据库中数据
C#入门代码_[Asp.Net教程]
Reading number is top 10 pictures
So beauty, will let you spray blood6
陪睡门马睿菈自曝写真 称首拍大尺度照片2
Beautiful Japanese beauty(漂亮的日本美女)2
女人化妆前后的对比
China's programmers are live what kind, had a look at will know that
浴室里的美女
The money of more than 100 countries and regions9
这才叫绝色美女1
大年初五接财神
The real super beauty2
Download software ranking
White deer villiage
Sora aoi 120 minutes
I for your crazy
Prostitutes diary
I'm come from Beijing2
The Bermuda triangle3
美女写真1
jBuilder2006
Unix video tutorial9
Sora aoi's film--cangkong_Blue.Sky
delv published in(发表于) 2014/1/6 9:05:26 Edit(编辑)
C#,3.0新特征之创建和初始化集合对象_[Asp.Net教程]

C#,3.0新特征之创建和初始化集合对象_[Asp.Net教程]

C# 3.0新特征之创建和初始化集合对象_[Asp.Net教程]

一、 引言

  C# 3.0中新的对象初始化器是一种简单的语法特征-借助于这种特征,对象的构建和初始化变得非常简单。假定你有一个类Student,它看起来有如下样子:


public class Student{
 public string firstName;
 public string lastName;
}

  那么,你可以使用对象初始化器来创建这个类的一个对象,如下所示:


var student1 = new Student{firstName = "Bruce", lastName = "Willis"};

  C# 3.0中新的集合初始化器语法也具有类似操作特征。例如,实现System.Collections.Generic.ICollection<T>的任何对象都可以使用一个集合初始化器来初始化它的值。

  一个集合初始化器由下列部分组成:

  · 一个对象初始化器序列,用"{"和"}"符号包括,并且由逗号隔开。

  · 元素初始化器,它们中每一个都指定一个元素以被添加到指定的集合对象中(元素初始化器不能是一个集合初始化器中的赋值表达式)。

  那么,究竟它是如何工作的呢?一个集合初始化器必须遵循下列规则:

  · 应用集合初始化器的每一个集合对象必须是一种实现了System.Collections.Generic.ICollection<T>的类型(恰好针对一个T)。

  · 必须存在从每一个元素初始化器到T类型的一种隐式转换。一个集合初始化器针对每一个指定的元素都会按序调用ICollection<T>.Add(T)方法。

  作为一个例子,下面的集合初始化器创建并初始化一个新的字符串集合-这个集合具有3个成员:"Alice","Bob"和"Chris",参考如下:


List<string> names = new List<string> { "Alice", "Bob", "Chris" };

  注意:所有的初始值都是字符串类型。否则,你将得到一个编译器错误。

  二、 实现集合初始化

  假定你想描述一个班及其中的注册同学。为此,你可以通过使用C# 3.0中的集合初始化器编程实现,如下所示:


using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;
namespace CollectionInitializer
{
 class Program
 {
  public class MyClass
  {
   public string nameofClass;
   public List<string> studentNames = new List<string>();
  }
  static void Main(string[] args)
  {
   var classes = new List<MyClass>
   {
    new MyClass
    {
     nameofClass = "Science",
     studentNames = {"Laura", "George"}
    },
    new MyClass
    {
     nameofClass = "Commerce",
     studentNames = {"Bill", "Hillary"}
    }
   };
  }
 }
}

  如果你有Visual Studio 2005并且安装了LINQ Preview,那么可以在IDE中编译上面的代码。

  如果你没有VS 2005但是安装了LINQ Preview,那么你可以使用下列命令来从命令行编译该代码:


C:\Program Files\LINQ Preview\Bin\Csc.exe
/reference:"C:\Program Files\LINQ Preview\Bin\System.Data.DLinq.dll"
/reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll
/reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll
/reference:"C:\Program Files\LINQ Preview\Bin\System.Query.dll"
/reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll
/reference:"C:\Program Files\LINQ Preview\Bin\System.Xml.XLinq.dll" Program.cs

  三、 代码剖析

  让我们更细致地分析一下前面的C# 3.0代码:


var classes = new List<MyClass>
{
 new MyClass
 {
  nameofClass = "Science",
  studentNames = {"Laura", "George"}
 },
 new MyClass
 {
  nameofClass = "Commerce",
  studentNames = {"Bill", "Hillary"}
 }
};

  对于编译器来说,它具有与下面代码相同的效果:


var classes = new List<MyClass>();
var __c1 = new MyClass ();
__c1.nameofClass = "Science";
__c1.studentNames.Add("Laura");
__c1.studentNames.Add("George");
classes.Add(__c1);
var __c2 = new MyClass();
__c2.nameofClass = "Commerce";
__c2.studentNames.Add("Bill");
__c2.studentNames.Add("Hillary");
classes.Add(__c2);

  如果激活ILDASM并且打开了编译的二进制代码形式,那么你能够看到如图1类似的内容。



图1.示例代码片断的编译的二进制形式

  如果你双击在ILDASM中的Main结点,那么你会看到下列代码:


.method private hidebysig static void Main(string[] args) cil managed
{
 .entrypoint
 //代码大小 138 (0x8a)
 .maxstack 3
 .locals init ([0] class [mscorlib]System.Collections.Generic.List`1
 <class CollectionInitializer.Program/MyClass>
 classes,
 [1] class [mscorlib]System.Collections.Generic.List`1
 <class CollectionInitializer.Program/MyClass>
 '<tampa>f__0',
 [2] class CollectionInitializer.Program/MyClass
 '<tampa>f__1',
 [3] class CollectionInitializer.Program/MyClass
 '<tampa>f__2')
 IL_0000: nop
 IL_0001: nop
 IL_0002: newobj instance void class [mscorlib]System.Collections.
 Generic.List`1<class CollectionInitializer.
 Program/MyClass>::.ctor()
 IL_0007: stloc.1
 IL_0008: ldloc.1
 IL_0009: nop
 IL_000a: newobj instance void CollectionInitializer.
 Program/MyClass::.ctor()
 IL_000f: stloc.2
 IL_0010: ldloc.2
 IL_0011: ldstr "Science"
 IL_0016: stfld string CollectionInitializer.
 Program/MyClass::nameofClass
 IL_001b: nop
 IL_001c: ldloc.2
 IL_001d: ldfld class [mscorlib]System.Collections.Generic.List`1
 <string> CollectionInitializer.
 Program/MyClass::studentNames
 IL_0022: ldstr "Laura"
 IL_0027: callvirt instance void class [mscorlib]System.
 Collections.Generic.List`1<string>::Add(!0)
 IL_002c: nop
 IL_002d: ldloc.2
 IL_002e: ldfld class [mscorlib]System.Collections.Generic.List`1
 <string> CollectionInitializer.
 Program/MyClass::studentNames
 IL_0033: ldstr "George"
 IL_0038: callvirt instance void class [mscorlib]System.Collections.
 Generic.List`1<string>::Add(!0)
 IL_003d: nop
 IL_003e: nop
 IL_003f: ldloc.2
 IL_0040: nop
 IL_0041: callvirt instance void class [mscorlib]System.Collections.
 Generic.List`1<class CollectionInitializer.
 Program/MyClass>::Add(!0)
 IL_0046: nop
 IL_0047: ldloc.1
 IL_0048: nop
 IL_0049: newobj instance void CollectionInitializer.
 Program/MyClass::.ctor()
 IL_004e: stloc.3
 IL_004f: ldloc.3
 IL_0050: ldstr "Commerce"
 IL_0055: stfld string CollectionInitializer.
 Program/MyClass::nameofClass
 IL_005a: nop
 IL_005b: ldloc.3
 IL_005c: ldfld class [mscorlib]System.Collections.Generic.List`1
 <string> CollectionInitializer.
 Program/MyClass::studentNames
 IL_0061: ldstr "Bill"
 IL_0066: callvirt instance void class [mscorlib]System.Collections.
 Generic.List`1<string>::Add(!0)
 IL_006b: nop
 IL_006c: ldloc.3
 IL_006d: ldfld class [mscorlib]System.Collections.Generic.List`1
 <string> CollectionInitializer.
 Program/MyClass::studentNames
 IL_0072: ldstr "Hillary"
 IL_0077: callvirt instance void class [mscorlib]System.Collections.
 Generic.List`1<string>::Add(!0)
 IL_007c: nop
 IL_007d: nop
 IL_007e: ldloc.3
 IL_007f: nop
 IL_0080: callvirt instance void class [mscorlib]System.Collections.
 Generic.List`1<class CollectionInitializer.
 Program/MyClass>::Add(!0)
 IL_0085: nop
 IL_0086: ldloc.1
 IL_0087: nop
 IL_0088: stloc.0
 IL_0089: ret
} //Program::Main方法结束

  四、 小结

  从前面的一些代码片断中你可以看到,C# 3.0在语法方面迈出了很大的一步。

  集合初始化器,作为C# 3.0新引入的特征之一,提供了一种新的语法来初始化集合对象。这种简单的语法把集合对象的创建和初始化结合到一个步骤中。
作者:朱先忠编译 来源:天极开发





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.