.NET 3.5 & VS2008 新特性

 

.NET 3.5 新特性

Wisdom Guo


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

 

2 JS 智能感知;

 

3. LINQLanguage 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

 

 

 

总的看来,.net 3.5以及VS2008的更新,变得不是很大。

最大的就是LINQ,其他的改动也是为了更好的支持这个new feature,看来微软是准备大力推行这个技术,不过是否会被各个企业采用还是要待时间检验(time will tell)。

----------

其他的,WCF, WPF,是不错的改变;

 

还有待学习,花费接近7小时时间,把这些新的知识看了一遍,还算不错。

 

wisdom

 

PS: 技术更新,你是永远赶不上的,所以掌握学习的方法,技术的本质,才是重点;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值