下表列出了visual C#中的更改可能在可防止應用程式在Visual Studio 2010的visual C#建立從生成或更改這些應用程式的運作時行為的 Visual Studio 2012。
類别 | 問題 | 說明 |
Lambda表達式 | 在循環體包含的lambda表達式中使用 foreach 語句中的疊代變量。 | 使用在嵌套lambda表達式的一個 foreach 疊代變量不再導緻意外的結果。 下面的示例在lambda表達式中使用變量的word。 C# 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。 C# 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 調用。 C# 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參數的通路方法。 | 當多查找某個解決方案,候選重載決策喜好命名參數的最具體的類型比對。 參數在調用不需要也不提供參數的考慮,僅當在重載候選類型比對的是同樣可行時。 在下面的示例中,string 比 p2的 object 是一種更好的類型。 是以,參數 p2 定義 ExampleMethod 的版本,則應挑選字元串,是以,即使它還有第三 params 參數。 C# 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>之間。 C# 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 = "a string";
CandidateMethod(() => { return dyn; });
}
}
// Output in Visual Studio 2012:
// Method that has a Func<object> parameter.
// Output in Visual Studio 2010:
// Method that has a Func<string> parameter.
|
原文位址: http://msdn.microsoft.com/zh-cn/library/hh678682.aspx
轉載于:https://www.cnblogs.com/WilliamWang/archive/2012/09/28/dotnet4_5.html