您的位置:首页 > 其它

.NET2.0新特性解读 .

2013-03-16 18:11 405 查看
一:与SQL SERVER2005集成(SQL SERVER2005中包含了CLR)

二:64位支持(32位环境下开发的代码可以在64位环境下运行)

三:泛型

定义:泛型指在多种数据类型上都可以操作

优点:

性能(使用System.Collections命令空间的集合类时,如果添加的是值类型,会进行装箱操作,读取时,会进行拆箱操作)

类型安全(使用泛型集合类,不允许一个集合中添加不同类型的元素)

泛型集合所有命名空间:System.Collections.Generic

泛型使用示例:(.NET2.0控制台程序)

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(".NET2.0新特性之泛型测试:");

new fanXin().useFX();

}

}

class fanXin

{

public void useFX()

{

System.Collections.Generic.Stack<string> myStack = new System.Collections.Generic.Stack<string>();

myStack.Push("St.Louis Rams");

myStack.Push("Indianapolis Colts");

myStack.Push("Minnersota Vikiings");

foreach (string item in myStack)

{

Console.WriteLine(item);

}

}

}

}

四:匿名方法(把编程步骤放在一个委托中,以后再执行该委托,而不是创建全新的方法)

示例:(.NET2.0 Winform程序)

添加一个Form窗体,在窗体上添加一个Button

namespace WindowsApplication1

{

partial class Form1

{

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.IContainer components = null;

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(244, 204);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(75, 23);

this.button1.TabIndex = 0;

this.button1.Text = "button1";

this.button1.UseVisualStyleBackColor = true;

//常规用法

this.button1.Click += new System.EventHandler(this.button1_Click);

//使用匿名方法

this.button1.Click += delegate(object mysender, System.EventArgs myArgs)

{

System.Windows.Forms.MessageBox.Show(".NET2.0新特性之匿名方法测试!");

};

//

// Form1

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(327, 242);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;

}

}

五:可空类型

可以创建能为空的值类型

示例:

class Program

{

static void Main(string[] args)

{

Console.WriteLine(".NET2.0新特性之可空类型测试:");

//可空类型定义方法:1 Nullable<类型> 变量名 2 类型? 变量名

Nullable<int> x = null;

//int? x=null;

if (!x.HasValue)

Console.WriteLine("空类型值:NULL");

else

Console.WriteLine(x.Value);

//??是接合运算符,为转换定义一个默认值,防止可空类型的值是null

int y = x ?? 0;

Console.WriteLine(y.ToString());

}

}

六:迭代器(允许在自已的定制类型中使用foreach循环,需要使类实现IEnumerable接口)

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(".NET2.0新特性之迭代器测试:");

myList list = new myList();

foreach (string item in list)

{

Console.WriteLine(item);

}

}

}

class myList

{

public IEnumerator<string> GetEnumerator()

{

//yield return Expression:提供一个枚举值

//yield break:终止枚举

yield return "St.Louis Rams";

yield return "Indianapolis Colts";

yield return "Minnersota Vikiings";

}

}

}

七:部分类(可以把单个类分解到多个类文件中,在编译时再把这些文件合并起来)

创建部分类:在类名前加上 partial关键字
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: