VS 2008 新特性一览

1)javacscript html css 智能感知,这个功能还是比较期待的
2)javascript调试功能
3)html编辑器在源文件和设计的基础增加split查看方式,这样就可以边改源文件边查看效果了
4)智能感知方面,在可选列表可见状态下按下Ctrl键,可以使列表透明,以查看被遮盖的部分,松开Ctrl后继续进行选择。
5)在创建项目之前,可以选择.net framework 的版本。这样,在使用vs 2008 强大IDE 的同时,可以创建 .基于net fx2.0 或 .net fx 3.0 的项目,而不用切换到vs2005下面。

重要的还是语言特性方面的改进:
1)扩展方法:
     扩展方法允许编程人员在静态类上编写自己的方法,以扩充静态类的功能。
     比如我们可以写这样一个方法:
     public static bool IsPrime(this int integer)
     {
           if(integer == 1)
            {
                   return false;
            }
            for(int i = 2;i <= Math.Sqrt(integer); i++)
            {
                    if(integer % 2 == 0)
                           return false; 
             }
             return true;
      }
之后就可以这么写:
      int i = 1798517;
      if(i.IsPrime())
      {
              Console.WriteLine(i);
      }

注意 static 和 this  关键字。这样做可以使程序更符合人的思维,更接近自然语言。看我们现在是怎么做的:
定义一个MyUtils类:
Class MyUtils
{
       public bool IsPrime(int integer)
       {
           if(integer == 1)
            {
                   return false;
            }
            for(int i = 2;i <= Math.Sqrt(integer); i++)
            {
                    if(integer % 2 == 0)
                           return false; 
            }
             return true;
      }
}
使用:
MyUtil mu = new MyUtil();
bool flag = mu.IsPrime(12517)
if(flag)
{
 ......
}
2) lambda表达式
.net 2.0 引进了匿名方法,.net 3.5进一步引进了lambda表达式来简化。
lambda表达式允许这样写:
IEnumerable <Book> cheapBooks = books.where(b => b.Price <=50)
(where 关键字是下面要提到的Linq中提供的功能)
相应的匿名方法:
IEnumerable <Book> cheapBooks = books.where(
                                                                 delegate(Book b)
                                                                 {
                                                                         return b.Price <=50;
                                                                }
                                                                );
3)Linq 语言集成查询(Language Integrated Query)
.net 3.5中增加了很多查询关键字,这样就可以直接编写查询,而不用再写sql语句。例如:
         var cheapBooks = from book in books
                                 orderby book.Price 
                                 select book.Name, book.Author,book.Price
                                 where book.Price <= 50
books可以使一个数组,可以使一个集合,可以是XML文件也可以是数据库里的一个表
4)自动属性
自动属性允许编程人员在定义一个类时作如下简化:
public class Book
{
        public string Name{ get; set; }
        public string Author{ get; set; }        
        public int    Price{ get; set; }
}
当然也可以没有set,如果属性是只读的话。这样就免去了定义私有的 _name, _author, _price及get set方法体,编译器会自动生成相应的私有变量和方法体。
5)对象初始化器,集合初始化器
在2.0中,我们这初始化一个对象;
Book book = new Book();
book.Name = "C# in a nutshell";
book.Author = "Peter Drayton";
book.Price = 30;
3.5中我们可以这么写:
Book book = new Book
{
        Name = "C# in a nutshell", Author = "Peter Drayton",Price = 30;
}
集合初始化器允许我们批量添加对象,而不用一条一条的用add方法添加:
List<Book> books = new List<book>
{
        new Book{Name = "C# in a nutshell", Author = "Peter Drayton",Price = 30;};
        new Book{Name = "Ajax in a nutshell", Author = "Peter Drayton",Price = 40;};
         new Book{Name = "Python in a nutshell", Author = "Peter Drayton",Price = 50;};
};
6)隐式本地变量
3.5中引入了var关键字,允许开发者在定义变量时不指定类型:
var a  = 15;
var b = new string[] {"a","b","c","d"}
但是c#仍然是强类型的,只不过编译器是根据后面所赋的值来推断出变量的类型,因此赋值是必须的,且只能有一种类型,定义后也就不能再进行改变,它在Linq 中非常有用,能极大地简化代码。
7)匿名变量
var w = new [] {"a","b","c","d"}
var b = new {Name  = "xml", Author = "aa",Price = 50};
C# 编译器会自动生成一个完整的匿名类包括私有字段,属性,get set,还有最后的赋值语句

posted @ 2008-02-22 22:45  ejiyuan  阅读(1780)  评论(0编辑  收藏  举报