Main 方法只有在它为空时才编译
Main method compiles only when it's empty
我创建了以下程序:
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";
}
}
}
这就是我到目前为止的全部内容。问题是它不是这样编译的。我收到以下异常:
} expected
只有当我将 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
:
The name 'args' does not exist in the current context
我真的不知道为什么我的项目会这样。有没有其他人遇到同样的问题?我该怎么办?
您不能在方法内声明 class 字段。
从其中移出以下行:
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";
假设你是直接复制代码,你主要犯了三个错误。首先,在您的枚举中,末尾有一些省略号:
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];
}
我创建了以下程序:
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";
}
}
}
这就是我到目前为止的全部内容。问题是它不是这样编译的。我收到以下异常:
} expected
只有当我将 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
:
The name 'args' does not exist in the current context
我真的不知道为什么我的项目会这样。有没有其他人遇到同样的问题?我该怎么办?
您不能在方法内声明 class 字段。
从其中移出以下行:
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";
假设你是直接复制代码,你主要犯了三个错误。首先,在您的枚举中,末尾有一些省略号:
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];
}