且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

方法中的方法

更新时间:2023-02-15 18:30:31

这个答案是在 C# 7 出来之前写的.使用 C# 7,您可以编写本地方法.

This answer was written before C# 7 came out. With C# 7 you can write local methods.

不,你不能那样做.您可以创建一个嵌套类:

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.