.NET 3.5 新特性介绍

1. 把Ajax extension集成到了Visual studio2008中;

2. JS 智能感知;

3. LINQ(Language Integrated Query

我们程序就是在维护一些数据,按照一定的逻辑;

所以:

  • 我们需要数据放在什么地方?
  • 以何种格式在存放?
  • 如何才能取得到数据?
  • 如何写数据?

但是访问每种数据的都有自己的方法,如:ado.net访问RMDB, xml DOM访问xml文件。。。

LINQ就是用来把这些不同的访问方式进行统一:一个一致的访问方式;

  • 到iteration的时候才进行取值,这样可以保证最新的结果集;

//user the IEnumberable interface

string[] array = {"wis","dd","aa","ad" };

IEnumerable<string> res = from s in array where s.StartsWith("a") orderby s select s;

foreach(string r in res)

{

Console.WriteLine(r);

}

//implicit to recieve the resut set

Console.WriteLine("-----from var list--------");

var res1 = from s in array where s.StartsWith("a") orderby s select s;

foreach (string r in res1)

{

Console.WriteLine(r);

}

Console.WriteLine("-----from var list after change one element--------");

//change the value in the array

array[0] = "ass";

//then do it again, you will see:

foreach (string r in res1)

{

Console.WriteLine(r);

}

Console.ReadLine();

  • 也可以通过convert来得到及时的结果:

//also you can conver the result set to get the immediate result

Console.WriteLine("-----from immediate result--------");

string[] resImme = (from s in array where s.StartsWith("a") orderby s select s).ToArray<string>();

foreach (string r in resImme)

{

Console.WriteLine(r);

}

  • 对于system.collections下的类因为没有实现IEnumberable,所以需要进行转换才可以使用LINQ:

Console.WriteLine("***** LINQ over ArrayList *****\n");

// Here is a nongeneric collection of cars.

ArrayList myCars = new ArrayList() {

new Car{ PetName = "Henry", Color = "Silver", Speed = 100, Make = "BMW"},

new Car{ PetName = "Daisy", Color = "Tan", Speed = 90, Make = "BMW"},

new Car{ PetName = "Mary", Color = "Black", Speed = 55, Make = "VW"},

new Car{ PetName = "Clunker", Color = "Rust", Speed = 5, Make = "Yugo"},

new Car{ PetName = "Melvin", Color = "White", Speed = 43, Make = "Ford"}

};

// Transform ArrayList into an IEnumerable<T>-compatible type.

IEnumerable<Car> myCarsEnum = myCars.OfType<Car>();

// Create a query expression.

var fastCars = from c in myCarsEnum where c.Speed > 55 select c;

foreach (var car in fastCars)

{

Console.WriteLine("{0} is going too fast!", car.PetName);

}

  • 在编译的时候,IDE会把from, where 这些进行转换;
  • LINQ可以运用许多聚合函数计算count,累加,差异等;
  • 对于查询条件可以用delegate进行:

Func<string, bool> searchFilter = new Func<string, bool>(Filter);

Func<string, string> itemToProcess = new Func<string,string>(ProcessItem);

// Pass the delegates into the methods of Enumerable.

var subset = currentVideoGames

.Where(searchFilter).OrderBy(itemToProcess).Select(itemToProcess);

4. Var 隐式类型:

    • 仅仅是用于定义local variables.
    • 不能用于数据成员,参数,返回值;
    • 声明的时候必须赋值;
    • 不能传递NULL;
    • 不允许var?
    • 一般不会使用var来声明一个变量,一般都是用在LINQ上中;

5. 自动的属性,可以在编译的时候,生成private data field;

· public string PetName { get; set; }—right

· public string PetName { get; }—wrong,必须get set同时,才可为自动的属性;

· 2005可以书写如下:

private string name;

public string Name

{

get

{

return name;

}

protected set

{

name = value;

}

}

· 2008中相当于:public string Name { protected set; get; }

6. 扩展方法:可以为编译好的程序集动态的添加方法;

· 必须是static类+static方法

· 只有且有第一个参数必须用this

static class TesterUtilClass

{

// Every Int32 now has a Foo() method...

public static void Foo(this int i)

{ Console.WriteLine("{0} called the Foo() method.", i); }

// ...which has been overloaded to take a string!

public static void Foo(this int i, string msg)

{ Console.WriteLine("{0} called Foo() and told me: {1}", i, msg); }

}

· 可以通过实例来调用扩展方法,也可以通过静态类;

Int A= 123;

A.Foo();

----Static way--------

TesterUtilClass.Foo(A);

· 扩展方法的scope,要注意;

· 其的命名空间,必须显示using。

· 可以构建一个扩展方法的DLL,可以极大提高重用度;

7. 扩展接口:

· 扩展类必须实现接口的中方法:

static class MathExtensions

{

// Extend IBasicMath this method and this

// implementation.

public static int Subtract(this IBasicMath itf,

int x, int y)

{

return x - y;

}

}

· 可以传入接口的实例,都可以调用这个扩展方法;

8. 局部方法 局部类型

· .net 2.0 局部类的作用大概一下:

o 类的功能过于多,不方便放在一个cs文件中;

o 多人同时写一个类;

· 总之partial type的作用就是可以把代码分离开放在不用的位置;

· .net 3.5中扩展了这个keyword,提供了局部的方法:

· 可以在另一个文件中实现,类的方法;

· 如果没有实现body,则不会被编译到程序中去;

· Partial methods can only be defined within a partial class.

· Partial methods must return void.

· Partial methods can be static or instance level.

· Partial methods can have arguments (including parameters modified by this, ref, or params—but not with the out modifier).

· Partial methods are always implicitly private.

9. 初始化:

//call the default constructor implicitly

· student s2 = new student { Name = "wisdom", Age = "23" };

· //explicitly

· student s = new student() {Name="wisdom", Age="23"};

· student s2 = new student("Male") { Name = "wisdom", Age = "23" };

· student.cs:

public class student

{

public string Name

{

set;

get;

}

public string Age

{

set;

get;

}

public string Gender

{

set;

get;

}

public student()

{

}

public student(string Gender)

{

this.Gender = Gender;

}

}

9.1 复合初始化:

ClassRoom cr = new ClassRoom()

{

name = "c1"

, students = new List<student>()

, student = new student()

{

Gender="male", Name="dd"

}

};

Class.cs 如下:

ClassRoom cr = new ClassRoom()

{

name = "c1"

,

students = new List<student>

{

new student{ Name="d"},

new student{ Gender="male"}

}

,

student = new student()

{

Gender = "male",

Name = "dd"

}

};

10.匿名类型:

//anonymous type

var anonyType = new { Name="d", Gender="male" };

Console.WriteLine(anonyType.Name,anonyType.Gender);

当不需要重用这个类的时候,就可以使用匿名类,很方便;

其他:

· You don’t control the name of the anonymous type.

· Anonymous types always extend System.Object.

· The fields and properties of an anonymous type are always read-only.

· Anonymous types cannot support events, custom methods, custom operators, or custom

· overrides.

· Anonymous types are always implicitly sealed.

· Anonymous types are always created using the default constructor.

匿名类可以嵌套:

//nested anonymous type

var anonyType = new { Name = "d", Gender = "male", sub = new {subName="sub"} };

Console.WriteLine(anonyType.Name,anonyType.Gender,anonyType.sub.subName);

11Lambda表达式

最大用处:用来替代匿名的方法,和代理

参数列表=>处理语句

I=> console.writeline(i);

WPF: Windows Presentation Foundation

WPF 是用来设计Windows form程序的,基于Xaml,把UI和逻辑完全剥离开;

WCF: Windows Communication Foundation

.NET 3.0 = .NET 2.0 + WCF + WPF + WCS + WF

作者来源:http://blog.csdn.net/wisdom521/archive/2008/08/18/2790866.aspx

posted @ 2011-02-21 08:42  Aamboo  阅读(1434)  评论(0编辑  收藏  举报