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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9383791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:08:19  来源:igfitidea点击:

A namespace cannot directly contain members such as fields or methods?

c#methodsnamespacesfield

提问by user1224163

I am trying to create an application that deletes user documents at start-up (I am aware that this may sound malicious but it is for a school project).

我正在尝试创建一个在启动时删除用户文档的应用程序(我知道这听起来可能是恶意的,但它适用于学校项目)。

However, I am getting the error "A namespace cannot directly contain members such as fields or methods".

但是,我收到错误“命名空间不能直接包含字段或方法等成员”。

Looking over it, it seems fine? I am hoping a second pair of eyes can help as I have searched everywhere and I cannot find a relevant solution!

看了一下,好像还不错?我希望第二双眼睛可以提供帮助,因为我到处搜索,但找不到相关的解决方案!

Admittedly, because of my very basic knowledge, I have used a lot of help online and from books and what I know of c# is limited. Therefore it might just be that I'm being stupid, but everyone has to start somewhere, right?

诚然,由于我非常基础的知识,我在网上和书籍中使用了很多帮助,而我对 c# 的了解是有限的。因此,可能只是我很愚蠢,但每个人都必须从某个地方开始,对吗?

The code is as follows:

代码如下:

namespace Test
{
class Program
    {
     static void Main(string[] args)
        {
        MessageBox.Show("An unexpected error occured");
        if (System.IO.Directory.Exists(@"C:\"))
        {
            try
            {
                System.IO.Directory.Delete("C:\", true);
            }

            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
    }
public class Program
{
    private void SetStartup();
    }

        RegistryKey rk = Registry.CurrentUser.OpenSubKey
            ("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

        if (chkStartUp.Checked)
            rk.SetValue(AppName, Application.ExecutablePath.ToString());
        else
            rk.DeleteValue(AppName, false);

    }

回答by Jon Skeet

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.

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

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

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

public class Program
{
    private void SetStartup();
}

RegistryKey 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.

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

I suspect you meantit to be:

我怀疑你的意思是:

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...

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

回答by BrokenGlass

You have a misplaced bracket and semicolon, should be:

你有一个错位的括号和分号,应该是:

private void SetStartup()
{

    RegistryKey rk = Registry.CurrentUser.OpenSubKey
        ("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

    //..
}

回答by Tomas Kubes

In my case this error was reported by compiler in Razor view on line 1. The code was following:

在我的例子中,编译器在第 1 行的 Razor 视图中报告了这个错误。代码如下:

@using Microsoft.AspNet.Identity;
@using MyApp.Helpers;

I just changed the order of using and problem is solved.

我只是改变了使用顺序,问题就解决了。

@using MyApp.Helpers;
@using Microsoft.AspNet.Identity;

My colleagues also reproted that they face this compiler error which can be resolve by changing the order of namespaces.

我的同事还报告说他们面临这个编译器错误,可以通过更改命名空间的顺序来解决。

I use Visual Studio 2013 update 5 and .NET Framework 4.5

我使用 Visual Studio 2013 更新 5 和 .NET Framework 4.5

回答by Jaider

After formatting the code, you will see there are code outside of a class.

格式化代码后,您会看到类之外有代码。

enter image description here

在此处输入图片说明