C# 7.0:本地方法
VS 2017 的 C# 7.0 中引入了本地方法,本地方法是一种语法糖,允许我们在方法内定义本地方法。更加类似于函数式语言,但是,本质上还是基于面向对象实现的。
1. 本地方法
先看一个示例:
using static System.Console; namespace UseLocalFunctions
{
class Program
{
static void Main(string[] args)
{
void Add(int x, int y)
{
WriteLine($"Sum of {x} and {y}: is {x + y}");
} void Multiply(int x, int y)
{
WriteLine($"Multiply of {x} and {y} is: {x * y}");
Add(, );
} Add(, );
Multiply(, ); ReadLine();
}
}
}
在此示例中,在 Main 方法内,嵌套定义了两个方法:Add 和 Multiply。这个方法可以在 Main 方法内被使用。这种方法被称为本地方法。英文称为:Local function.
使用 ILDasm 工具,可以看到编译之后的结果。
这两个本地方法被翻译成了两个静态的私有方法,它只能在定义的方法内被调用。
本地方法的语法定义为:
<modifiers: async | unsafe> <return-type> <method-name> <parameter-list>
方法的修饰符只有两种:async 和 unsafe,所有的本地方法都是私有的
- 如果您使用了 private 修饰,会收到 编译器的错误提示:error CS0106, "The modifier 'static' is not valid for this item."
- 如果您使用了 static,会收到编译器的错误提示:error CS0106, "The modifier 'static' is not valid for this item."
2. 带有返回类型的本地方法
本地方法也可以带有返回类型。如果类型用错的话,Visual Studio 可以给出提示。
class Program
{
static void Main(string[] args)
{
PrintStudentMarks(,
new Subject
{
SubjectName = "Math",
Marks =
}, new Subject
{
SubjectName = "physics",
Marks =
}, new Subject
{
SubjectName = "Chem",
Marks =
}); ReadLine();
} public static void PrintStudentMarks(int studentId, params Subject[] subjects)
{
WriteLine($"Student Id{studentId} Total Marks: {CalculateMarks()}");
WriteLine($"Student wise marks");
foreach(var subject in subjects)
{
WriteLine($"Subject Name: {subject.SubjectName}\t Marks: {subject.Marks}");
} decimal CalculateMarks()
{
decimal totalMarks = ;
foreach(var subject in subjects)
{
totalMarks += subject.Marks;
} return totalMarks;
}
} public class Subject
{
public string SubjectName
{
get; set;
} public decimal Marks
{
get; set;
}
}
}
3. 使用本地方法实现递归
本地方法不需要维护调用堆栈,而递归方法需要维护调用堆栈,本地方法效率更高。下面的示例演示了两种方法的区别。
注意:该示例使用了类型 BigInteger ,需要添加对程序集 System.Numeric.dll 的引用。
代码如下。
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
BigInteger f1 = GetFactorialUsingLocal();
watch.Stop();
WriteLine($"Using local function: {watch.ElapsedTicks}"); watch.Reset();
watch.Start();
BigInteger f2 = GetFactorial();
watch.Stop();
WriteLine($"Using recursive function: {watch.ElapsedTicks}");
} private static BigInteger GetFactorialUsingLocal(int number)
{
if (number < )
throw new ArgumentException("negative number", nameof(number));
else if (number == )
return ;
BigInteger result = number;
while (number > )
{
Multiply(number - );
number--;
} void Multiply(int x) => result *= x;
return result;
} private static BigInteger GetFactorial(int number)
{
if (number < )
throw new ArgumentException("nagative number", nameof(number));
return number == ? : number * GetFactorial(number - );
}
}
在我的机器上,结果如下:
Using local function: 181770
Using recursive function: 456602
可以看到两者之间的性能差异。
此时,为了传递 result ,在生成的代码中,编译器会自动做一些额外的工作。
4. 本地方法与 Lambda 的比较
1. 性能
当创建 Lambda 的时候,将会创建一个委托,这需要内存分配,因为委托是一个对象。而本地方法则不需要,它是一个真正的方法。
另外,本地方法可以更为有效地使用本地变量,Lambda 将变量放到类中,而本地方法可以使用结构,而不使用内存分配。
这意味着调用本地方法更为节约且可能内联。
2. 本地方法可以递归
Lambda 也可以实现递归,但是代码丑陋,您需要先赋予 lambda 为 null。本地方法可以更为自然地递归。
3. 本地方法可以使用泛型
Lambda 不能使用泛型。这是因为需要赋予一个实例类型的变量。
4. 本地方法可以实现迭代器
Lambda 不能使用 yield return (以及 yield break)关键字,以实现 IEnumerable<T> 返回函数。本地方法可以。