日期:2014-05-20  浏览次数:20932 次

探索!全新一代 Visual Studio 11[持续更新]
今天微软发布了Visual Studio 2011 Developer Preview,由于糟糕的线路,暂时还在下载中。

计划下载之后第一时间向大家汇报 VS 11 的体验感受。

已经下载完成,稍后安装。不过大家注意,只有Windows 7、Windows Server 2008 R2和Windows (Server) Developer Preview支持VS11,如果你还在使用Windows XP、Windows Server 2003和Windows Vista,那么很遗憾,在你打算升级操作系统之前就不用下载VS 11了。


先给出一些新特性,这些足够激动人心!

先看Visual Basic
Visual Basic 缺少一个 C# 2005 开始增加的特性,就是迭代器。
在C#中,我们可以使用yield return来实现迭代器,比如:
C# code
IEnumerable<string> split(string, source, char c)
{
    string s = "";
    for (int i = 0; i < source.Length; i++)
    {
        if (source[i] == c)
        {
            yield return s;
            s = "";
        }
        else
        {
            s += new string(new char[] { source[i] });
        }
    }
    if (s != "") yield return s;
}

调用:
C# code
foreach (string s in split("hello world csharp", ' '))
{
    Console.WriteLine(s);
}


在Visual Basic中,很难实现这样的功能,一种简单而且稳妥的办法是,使用List<string>代替IEnumerable<string>,这样我们就只能在所有项都找到以后才开始迭代了。

之所以迭代器难以实现,是因为C#编译器在内部为我们的迭代器创建了一个线程,并且使用了从循环外向循环内跳转的转移指令,这样就可以在迭代触发的时候保存当前堆栈,这很难用合法的VB语法描述。

现在,Visual Basic 11,终于支持yield,而且写法比C#更简单:
VB.NET code
Sub Main()
    For Each number As Integer In SomeNumbers()
        Console.Write(number & " ")
    Next
    ' Output: 3 5 8
    Console.ReadKey()
End Sub

Private Iterator Function SomeNumbers() As System.Collections.IEnumerable
    ' Use multiple yield statements.
    Yield 3
    Yield 5
    Yield 8
End Function




对于C#来说,最激动人心的增强是异步语法,异步语法有2大用途,一个是增强程序的并行计算能力,提高性能。另一个是在下一代应用程序(包括Windows 8的程序),异步从服务器端获取数据并且处理成为一种通用的、常见的代码架构风格。客户机和服务器的通讯延迟的不确定性使得那种传统的Web程序(延迟放在服务器端)和桌面程序(几乎没有延迟,或者有可控的延迟)以及架构成为落伍的东西。

而且高兴的是,异步程序编写在C# vNext上非常简单,微软的文档指出,将同步的代码转换为异步只需要简单的几步

为方法加上 async 修饰

将返回值从 byte[] 转换为 Task<byte[]>

在方法调用前加上 "Async

在方法体内作如下修改

将方法由同步的 Get 变成异步的 GetAsync

对结果使用 await 运算符

就这些!看代码:

C# code
// Synchronous version of a method that downloads the resource that a URI
// links to and accesses its content.
private byte[] GetByteArray(Uri currentURI)
{
    // Declare an HttpClient object and increase the buffer size. 
    HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 1000000 };

    // The Get method returns an HttpResponseMessage that contains the
    // content of the resource along with other information.
    HttpResponseMessage httpRM = client.Get(currentURI);

    // Use ReadAsByteArray to access the content as a byte array.
    return httpRM.Content.ReadAsByteArray();
}

// Asynchronous version of the same method.
private async Task<byte[]> GetByteArrayAsync(Uri currentURI)
{
    // Declare an HttpClient object and increase the buffer size. 
    HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 1000000 };

    // The GetAsync method returns a Task(Of T), where T is an HttpResponseMessage. 
    HttpResponseMessage httpRM = await client.GetAsync(currentURI);

    // Use ReadAsByteArray to access the content as a byte array.
    return httpRM.Content.ReadAsByteArray();
}




关于异步编程,我将在第一时间体验并且给出更详细的介绍。

对于F#,最大的改进是查询表达式。
看下面的代码
C# code
[<Generate>]
type Northwind = ODataService<"http://services.odata.org/Northwind/Northwind.svc">
let db = new Northwind.NorthwindEntities()

// A query expression.
let query1 = query { for customer in db.Customers do
                     select customer }

query1
|> Seq.iter (fun customer -> printfn "Company: %s Contact: %s" customer.CompanyName customer.ContactName)