所以我读过这篇文章C# Overloads说你可以使用重载方法来消除复杂性并提高性能,它们提供了以下示例:
使用系统;
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);
}
}
还是最后一段代码等同于第一段代码?