【问题标题】:Main method compiles only when it's empty [closed]Main 方法仅在为空时编译[关闭]
【发布时间】:2017-01-12 14:22:44
【问题描述】:

我创建了以下程序:

namespace MyNamespace
{
    public enum MyEnum { Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6, Jul = 7, Aug = 8, Sep = 9, Okt = 10, Nov = 11, Dec = 12 }

    class Program
    {
        static void Main(string[] args)
        {
            private static string sourcePath = @"T:\his\is\my\source";
            private static string destinationPath = @"T:\his\is\my\destination";
        }
    }
}

到目前为止,这就是我所拥有的。问题是它不能像这样编译。我得到以下异常:

} 预期

仅当我将 Main 方法主体留空时才有效。当我使用 Ctrl + KD 格式化代码时,它的格式是这样的:

namespace MyNamespace
{
    public enum MyEnum { SomeField = 1, SomeOtherField = 2, ... }

    class Program
    {
        static void Main(string[] args)
        {
            private static string sourcePath = @"T:\his\is\my\source";
        private static string destinationPath = @"T:\his\is\my\destination";
    }
}
}

这完全没有任何意义。我什至无法访问参数args

当前上下文中不存在名称“args”

我真的不知道我的项目为什么会这样。有没有其他人遇到过同样的问题?我该怎么办?

【问题讨论】:

  • 会不会是这条线? public enum MyEnum { SomeField = 1, SomeOtherField = 2, ... }。你真的有这样的吗?去掉省略号,输入实际值。
  • 不,即使我删除了枚举,我也会遇到同样的异常。
  • @rory.ap 呃,这就是问题所在! ;p

标签: c# console-application


【解决方案1】:

您不能在方法中声明类字段。
将以下几行移出:

private static string sourcePath = @"T:\his\is\my\source";
private static string destinationPath = @"T:\his\is\my\destination";

或者,如果您希望这些变量是方法的本地变量,请将它们声明为:

string sourcePath = @"T:\his\is\my\source";
string destinationPath = @"T:\his\is\my\destination";

【讨论】:

  • 而且还是会失败...
  • 谢谢,现在可以使用了。
  • @"T:\h..." 请加@
  • @Bauss:这些字符串不是必需的constants
  • 为什么要改成const,解决问题??这其中的逻辑是什么?
【解决方案2】:

假设你直接复制了代码,你有三个主要错误。首先,在您的枚举中,最后有一些省略号:

public enum MyEnum { SomeField = 1, SomeOtherField = 2, ... }

您需要删除它们,并在必要时输入更多值。

其次,您试图在 Main 方法中声明看起来像字段的内容。您需要将它们移出方法:

private static string sourcePath = "T:\his\is\my\source";
private static string destinationPath = "T:\his\is\my\destination";

static void Main(string[] args)
{

}

或者通过删除访问级别修饰符和静态修饰符使它们成为方法级别的变量:

static void Main(string[] args)
{
    string sourcePath = "T:\his\is\my\source";
    string destinationPath = "T:\his\is\my\destination";
}

最后你的路径是这样构造的:

"T:\his\is\my\source"

这里有反斜杠,在 C# 字符串中用于创建转义序列。您需要用另一个反斜杠转义它们:

"T:\\his\\is\\my\\source"

或在它们上使用@ 修饰符,以便逐字处理它们,see this question for more information.

@"T:\his\is\my\source"

旁注:最好将路径作为命令行参数传递,或者使用配置文件,而不是将它们硬编码到代码中。例如(未经测试的代码):

static void Main(string[] args)
{
    if (args.Length < 2)
    {
        throw new ArgumentOutOfRangeException("Two arguments must be supplied!");
    }

    //Add error checking as appropriate
    //You could also format the arguments like -s:... and -d:...
    //Then the order of the arguments will not matter

    string sourcePath = args[0];
    string destinationPath = args[1];
}

【讨论】:

  • 我添加了省略号只是为了表明还有其他字段。我的实际代码中没有它们。问题是 Dmitry 已经提到的变量声明。
  • @diiN__________ 请注意我的开场评论“假设您已直接复制代码”并且您发布的代码存在 3 个主要错误,而 Dmitry 仅显示了一个。下次发布minimal reproducible example 否则你会得到额外的信息,说明你的代码有什么问题,就像我发布的那样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 2023-03-16
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多