且构网

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

命名空间不能直接包含成员,如字段或方法?

更新时间:2022-04-22 22:28:01

您的代码在 SetStartup 周围严重混乱.如果您遵循正常的缩进,您会更清楚地看到正在发生的事情.在 Visual Studio 中按 Ctrl-E 后按 D,它会重新格式化您的文档 - 这应该会使事情变得更加清晰.

Your code is seriously messed up around SetStartup. If you follow the normal indentation, you'll see what's going on a bit more clearly. Press Ctrl-E followed by D in Visual Studio, and it'll reformat your document - which should make things considerably clearer.

看看这个(在我缩进之后):

Look at this (after I've indented it):

public class Program
{
    private void SetStartup();
}

RegistryKey rk = [...];

那是试图在类之外声明一个变量 (rk).您还有一个没有主体的非抽象方法,并且最后缺少右大括号.

That's trying to declare a variable (rk) outside a class. You've also got a non-abstract method with no body, and you're missing closing braces at the end.

我怀疑你的意思是:

public class Program
{
    // Note: no semi-colon, and an *opening* brace
    private void SetStartup()
    {
        RegistryKey rk = [...];
        // Other code
    }
}

// And you'd want to close the namespace declaration too

你也会在声明两个(非部分)同名类时遇到问题......

You're also going to have problems declaring two (non-partial) classes with the same name...