我正在创建一个带有一些可重用代码的 C# 库,并尝试在方法内创建一个方法。我有一个这样的方法:
public static void Method1()
{
// Code
}
我想做的是这样的:
public static void Method1()
{
public static void Method2()
{
}
public static void Method3()
{
}
}
那么我可以选择Method1.Method2
or Method1.Method3
。显然编译器对此并不满意,非常感谢任何帮助。谢谢。
如果通过嵌套方法,您的意思是只能在该方法内调用的方法(例如在 Delphi 中),您可以使用委托。
public static void Method1()
{
var method2 = new Action(() => { /* action body */ } );
var method3 = new Action(() => { /* action body */ } );
//call them like normal methods
method2();
method3();
//if you want an argument
var actionWithArgument = new Action<int>(i => { Console.WriteLine(i); });
actionWithArgument(5);
//if you want to return something
var function = new Func<int, int>(i => { return i++; });
int test = function(6);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)