所以我讀這篇文章C# Overloads它說,你可以使用重載的方法來消除複雜性和提高性能,他們提供了下面的例子:C# - 重載性能增強的查詢
使用系統;
class Program
{
public static void Main()
{
ShowString(string.Empty);
ShowString("Category");
}
static void ShowString(string value)
{
if (value == string.Empty)
{
Console.WriteLine("Popular");
}
else
{
Console.WriteLine(value);
}
}
}
他們然後使用重寫重載:
using System;
class Program
{
public static void Main()
{
ShowString();
ShowString("Category");
}
static void ShowString()
{
// Send default argument to overload.
ShowString("Popular");
}
static void ShowString(string value)
{
// We don't need an if check here, which makes
// ... calling this method directly faster.
Console.WriteLine(value);
}
}
會使用重載變體比使用方法與默認參數值提供更好的性能?
像:
using System;
class Program
{
public static void Main()
{
ShowString();
ShowString("Category");
}
static void ShowString(string value = "Popular")
{
// We don't need an if check here, which makes
// ... calling this method directly faster.
Console.WriteLine(value);
}
}
或是代碼的最後塊的代碼的第一個塊的等效?
性能是很少用於語言特徵的度量。 – 2012-08-11 13:12:13