C# 方法中的方法
我正在用一些可重用的代码创建一个C#库,并试图在一个方法中创建一个方法。我有这样一种方法:C# 方法中的方法,c#,methods,C#,Methods,我正在用一些可重用的代码创建一个C#库,并试图在一个方法中创建一个方法。我有这样一种方法: public static void Method1() { // Code } 我想做的是: public static void Method1() { public static void Method2() { } public static void Method3() { } } 然后我可以选择Method1.Method2或Method1.Met
public static void Method1()
{
// Code
}
我想做的是:
public static void Method1()
{
public static void Method2()
{
}
public static void Method3()
{
}
}
然后我可以选择Method1.Method2
或Method1.Method3
。显然,编译器对此不满意,非常感谢任何帮助。谢谢。你快到了
public static void Method1()
应该是
public static class Method1{}
这个答案是在C#7问世之前写的。使用C#7,您可以编写本地方法。 不,你不能那样做。您可以创建嵌套类:
public class ContainingClass
{
public static class NestedClass
{
public static void Method2()
{
}
public static void Method3()
{
}
}
}
然后你会打电话:
ContainingClass.NestedClass.Method2();
或
不过我不推荐这个。通常,使用公共嵌套类型是个坏主意
你能告诉我们更多你想要达到的目标吗?很可能有更好的方法。为什么不使用类
public static class Helper
{
public static string MethodA()
{
return "A";
}
public static string MethodA()
{
return "A";
}
}
现在你可以通过
Helper.MethodA();
若所说的嵌套方法是指仅可在该方法内调用的方法(如在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);
}
publicstaticvoidmethod1()
{
var method2=新操作(()=>{/*操作体*/});
var method3=新操作(()=>{/*操作体*/});
//像普通方法一样调用它们
方法2();
方法3();
//如果你想争论
var actionWithArgument=新操作(i=>{Console.WriteLine(i);});
有争议的诉讼(5);
//如果你想退货
var function=newfunc(i=>{return i++;});
int测试=功能(6);
}
您可以使用完整的代码在方法中定义委托,并根据需要调用它们
public class MyMethods
{
public void Method1()
{
// defining your methods
Action method1 = new Action( () =>
{
Console.WriteLine("I am method 1");
Thread.Sleep(100);
var b = 3.14;
Console.WriteLine(b);
}
);
Action<int> method2 = new Action<int>( a =>
{
Console.WriteLine("I am method 2");
Console.WriteLine(a);
}
);
Func<int, bool> method3 = new Func<int, bool>( a =>
{
Console.WriteLine("I am a function");
return a > 10;
}
);
// calling your methods
method1.Invoke();
method2.Invoke(10);
method3.Invoke(5);
}
}
您不想改用嵌套类吗
也就是说,您似乎不尊重方法,因为您希望一个方法一次可以做多件事。为什么不在另一个方法中运行一个方法呢 公共空间M1() { 做事 } 公共空间M1() { 做事 M1(); }是,当发布
C#7.0
时,将允许您这样做。您将能够在方法中拥有一个方法,如下所示:
public int GetName(int userId)
{
int GetFamilyName(int id)
{
return User.FamilyName;
}
string firstName = User.FirstName;
var fullName = firstName + GetFamilyName(userId);
return fullName;
}
从C#7.0开始,您可以这样做:
public static void SlimShady()
{
void Hi([CallerMemberName] string name = null)
{
Console.WriteLine($"Hi! My name is {name}");
}
Hi();
}
这就是所谓的本地函数,这正是您所需要的
我从中获取了示例,但可以找到更多信息。旧线程,但C#确实有嵌套函数的概念
Func<int> getCalcFunction(int total, bool useAddition)
{
int overallValue = 0;
if (useAddition)
{
Func<int> incrementer = new Func<int>(() =>
{
overallValue += total;
return overallValue;
});
return incrementer;
}
else
{
Func<int> decrementer = new Func<int>(() =>
{
overallValue -= total;
return overallValue;
});
return decrementer;
}
}
private void CalcTotals()
{
Func<int> decrem = getCalcFunction(30, false);
int a = decrem(); //result = -30
a = decrem(); //result = -60
Func<int> increm = getCalcFunction(30, true);
int b = increm(); //result = 30
b = increm(); //result = 60
}
Func getCalcFunction(整数合计,布尔使用加法)
{
int总值=0;
如果(使用添加)
{
Func incrementer=新函数(()=>
{
总价值+=总价值;
返回总体值;
});
返回递增器;
}
其他的
{
Func递减器=新Func(()=>
{
总价值-=总价值;
返回总体值;
});
返回减量器;
}
}
私有无效CalcTotals()
{
Func decrem=getCalcFunction(30,false);
int a=decrem();//结果=-30
a=decrem();//结果=-60
Func increm=getCalcFunction(30,真);
int b=increm();//结果=30
b=increm();//结果=60
}
方法中的方法概念在C#中如何工作?我最后一次看到嵌套函数/方法定义是在PHP和JavaScript中,但它们是用来动态创建顶级函数的。将这两个方法放在一个静态类中。您想用它实现什么?很简单,您只能从该方法调用它。Delphi支持它。在Delphi中您可以这样做,但是嵌套方法将是“私有”的,只有父方法才能访问它。无论如何,我错过了很多delphi/pascal特性来获得更干净、更有条理的代码。我想调用一个方法,然后选择如何处理代码。例如,我将一个字符串参数传递给该方法,然后我可以调用两个嵌套方法中的任意一个,这取决于我想对字符串执行的操作,可能一个方法将转换为int,另一个方法将转换为double,类似的。Thanks@BaliC:我可能会使用单独的私有方法。您可以在public方法中使用匿名函数,但只需将它们设置为私有并记录预期用途可能会更简单。谢谢,这正是我需要帮助的地方,我不确定该怎么做。该示例没有显示类似pascal的嵌套函数的核心功能,即从嵌套函数访问父变量?将其分解到其他实体是无用的,因为您必须通过参数传递所有内容again@Andy:好吧,几乎是这样-但肯定不是在2011年:)方便的参考链接:我爱代表让生活如此轻松。。。哦,这应该是答案。这个解决方案是在c#中使用嵌套函数的一种确定方法。参考维基百科:这实际上是刚刚创建的lambda函数,不是吗?它不仅不会编译,而且如果编译,还会导致堆栈溢出。这不是一个好主意,很高兴知道。在本例中,是否可以在嵌套的GetFamilyName()函数中使用userId值而不将其作为参数传递?@symbont:是的,主函数的参数(和局部变量)对局部函数可见(类似于Delphi)。请参阅中图像中的第二个示例,当它被添加为IIRC时,可能值得一提这是最近添加的,可能在每个人的环境中都不可用。另外,calCrithmatic
可能不是返回函数且不进行实际运算的函数的最佳名称。我认为这两点都是有效的。我更新了方法名。
public static void SlimShady()
{
void Hi([CallerMemberName] string name = null)
{
Console.WriteLine($"Hi! My name is {name}");
}
Hi();
}
Func<int> getCalcFunction(int total, bool useAddition)
{
int overallValue = 0;
if (useAddition)
{
Func<int> incrementer = new Func<int>(() =>
{
overallValue += total;
return overallValue;
});
return incrementer;
}
else
{
Func<int> decrementer = new Func<int>(() =>
{
overallValue -= total;
return overallValue;
});
return decrementer;
}
}
private void CalcTotals()
{
Func<int> decrem = getCalcFunction(30, false);
int a = decrem(); //result = -30
a = decrem(); //result = -60
Func<int> increm = getCalcFunction(30, true);
int b = increm(); //result = 30
b = increm(); //result = 60
}