中的一个方法方法 [英] Method Within A Method
问题描述
我创造了一些可重用的代码一个C#库,并试图创建一个方法中的方法。我有这样一个方法:
I am creating a C# library with some reusable code and was trying to create a method inside a method. I have a method like this:
public static void Method1()
{
// Code
}
我想什么做的是这样的:
What I would like to do is this:
public static void Method1()
{
public static void Method2()
{
}
public static void Method3()
{
}
}
然后,我可以选择使用 Method1.Method2
或 Method1.Method3
。显然,编译器不会对此感到高兴,任何帮助深表感谢。谢谢你。
Then I could choose either Method1.Method2
or Method1.Method3
. Obviously the compiler isn't happy about this, any help is much appreciated. Thanks.
推荐答案
没有,你不能这样做。您的可能的创建一个嵌套类:
No, you can't do that. You could create a nested class:
public class ContainingClass
{
public static class NestedClass
{
public static void Method2()
{
}
public static void Method3()
{
}
}
}
然后你会打电话:
ContainingClass.NestedClass.Method2();
或
ContainingClass.NestedClass.Method3();
我的不会的建议,虽然这个。 一般的这是一个坏主意,有公共嵌套类型。
I wouldn't recommend this though. Usually it's a bad idea to have public nested types.
你能告诉我们更多关于你想达到什么样的?有可能是一个更好的方法。
Can you tell us more about what you're trying to achieve? There may well be a better approach.
这篇关于中的一个方法方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!