zoukankan      html  css  js  c++  java
  • Visual Studio 2012 中 Visual C# 的重大更改

    http://msdn.microsoft.com/zh-cn/library/hh678682(v=vs.110).aspx

    下表列出了 Visual C# 中的更改可能在可防止应用程序在 Visual Studio 中的 Visual c# 2010 创建从生成或更改这些应用程序的运行时行为的 Visual Studio 2012。

     

    类别

    问题

    描述

    Lambda 表达式

    在循环体包含的 lambda 表达式中使用 foreach语句中的迭代变量。

    使用在嵌套 lambda 表达式的一个 foreach 迭代变量不再导致意外的结果。 下面的示例在 lambda 表达式中使用变量的 word

     
    static void Main()
    {
        var methods = new List<Action>();
        foreach (var word in new string[] { "hello", "world" })
        {
            methods.Add(() => Console.Write(word + " "));
        }
    
        methods[0]();
        methods[1]();
    }
    
    // Output in Visual Studio 2012: 
    // hello world
    
    // Output in Visual Studio 2010: 
    // world world
    
    

    LINQ 表达式

    在循环体包含的 LINQ 表达式中使用 foreach 语句中的迭代变量。

    使用在 LINQ 表达式的一个 foreach 迭代变量不再导致意外的结果。 下面的示例在 LINQ 查询中使用可变 number

     
    static void Main()
    {
        var lines = new List<IEnumerable<string>>(); 
        int[] numbers = { 1, 2, 3 };
        char[] letters = { 'a', 'b', 'c' };
    
        foreach (var number in numbers)
        {
            var line = from letter in letters
                       select number.ToString() + letter;
    
            lines.Add(line);
        }
    
        foreach (var line in lines)
        {
            foreach (var entry in line)
                Console.Write(entry + " ");
            Console.WriteLine();
        }
    }
    // Output in Visual Studio 2012: 
    // 1a 1b 1c
    // 2a 2b 2c
    // 3a 3b 3c
    
    // Output in Visual Studio 2010: 
    // 3a 3b 3c
    // 3a 3b 3c
    // 3a 3b 3c
    

    命名实参

    从名为和位置实参的副作用在方法调用现在却从左至右参数列表。

    在方法合并的名称和位置实参的副作用在调用语句的调用现在生成从左至右参数列表。 使用命名和位置的参数组不同的顺序,在下面的示例中,TestMethod 调用。

     
    class Program
    {
        static void Main(string[] args)
        {
            TestMethod(WriteLetter("A"), b: WriteLetter("B"), c: WriteLetter("C"));
            TestMethod(WriteLetter("A"), c: WriteLetter("C"), b: WriteLetter("B"));
        }
    
        static int WriteLetter(string letter)
        {
            Console.Write(letter + " ");
            return 1;
        }
    
        static void TestMethod(int a, int b, int c)
        { }
    
        // Output in Visual Studio 2012:
        // A B C A C B
    
        // Output in Visual Studio 2010:
        // B C A C B A
    }
    
    

    超加载解决方法

    超加载解析改进了对该使用 命名实参 到包含param 参数的访问方法。

    当多查找某个解析候选,即加载解析喜好命名参数的最具体的类型匹配。 参数在调用不需要也不提供参数的考虑,只有 + 当在 hyper-v 加载的候选类型匹配是同样可行时。

    在下面的示例中,string 比 p2的 object 是一种更好的类型。 因此,参数 p2 定义 ExampleMethod 的版本,则应挑选字符串,因此,即使它还有第三params 参数。

     
    class Program
    {
        static void Main(string[] args)
        {
            ExampleMethod(p2: "");
        }
    
        public static void ExampleMethod(string p1 = null, object p2 = null)
        {
            Console.WriteLine("ExampleMethod: p2 is object");
        }
        public static void ExampleMethod(string p2 = null, object p1 = null, params int[] p3)
        {
            Console.WriteLine("ExampleMethod: p2 is string");
        }
    }
    
    // Output in Visual Studio 2012:
    // ExampleMethod: p2 is string
    
    // Output in Visual Studio 2010:
    // ExampleMethod: p2 is object
    
    

    超加载解决方法

    超加载解析改进用于调用算法必须选择在Func<object>参数和具有不同的类型参数的Func 参数之间的位置 (即,string 或int?)Func<dynamic>参数的。

    在下面的示例中,发送到 CandidateMethod 的调用 Func<dynamic> 参数有两个解析候选对象。 对应的形参在某个候选函数是 Func<object>,因此,在其他的相应参数是 Func<string>

    有一个 Func<object> 参数的超加载候选应处于选中状态,因为 object 和 dynamic 视为等效的。 因此,标识转换存在不仅在 dynamic 和 object 之间,还可以在构造类型 Func<dynamic> 和 Func<object>之间。

     
    class Program
    {
        public static void CandidateMethod(Func<object> fun)
        {
            Console.WriteLine("Method that has a Func<object> parameter.");
        }
    
        public static void CandidateMethod(Func<string> fun)
        {
            Console.WriteLine("Method that has a Func<string> parameter.");
        }
    
        static void Main(string[] args)
        {
            dynamic dyn = 15;
            CandidateMethod(() => { return dyn; });
        }
    }
    // Output in Visual Studio 2012:
    // Method that has a Func<object> parameter.
    
    // Output in Visual Studio 2010 if Microsoft.CSharp is referenced:
    // Method that has a Func<string> parameter.
    
    // Output in Visual Studio 2010 if Microsoft.CSharp isn't referenced (for instance, in a Unit Test Project):
    // Method that has a Func<object> parameter.
    

     

  • 相关阅读:
    网络爬虫之框架(Scrapy)
    模拟投币试验
    [LeetCode#177]Nth Highest Salary
    Windows Server 2008 各版本功能差异与比较各版本概观--转载
    Win2008 R2下Server Core常用命令小结
    powershell 中用Sqlps管理我台sqlserver 2008r2
    初识 Markdown
    React 入门(3): 严格模式 ReactDOM
    ES6 类的正确定义方式 公有类字段 getter / setter
    Lodash 去抖动 节流
  • 原文地址:https://www.cnblogs.com/zhiji6/p/2993703.html
Copyright © 2011-2022 走看看