在 C# 中从静态 Main 传递参数
本文关键字:参数 Main 静态 | 更新日期: 2023-09-27 18:34:57
我是 C# 新手,并且在将字段从静态 Main 方法传递到另一个方法时遇到问题。
这是代码。我已经复制了相关行末尾的错误。 使用 VS2012。
namespace SpaceApiTest
{
class SpaceApiTest
{
static void Main(string[] args)
{
Input input = new Input();
input.debug = true; // error CS1513: } expected
public int getIp(ref Input input)
{
input.ip.Add("192.168.119.2");
return 0;
}
SpaceApiTest st = new SpaceApiTest();
st.getIp(input); // error CS1519: Invalid token '(' in class, struct, or interface
member declaration
// Invalid token ')' in class, struct, or interface member declaration
}
}
public struct Input
{
public string ip;
public string token;
public bool debug;
}
} // error CS1022: Type or namespace definition, or end-of-file expected
您收到该错误是因为方法中有一个方法。试试这个:
static void Main(string[] args)
{
Input input = new Input();
input.debug = true;
SpaceApiTest st = new SpaceApiTest();
st.GetIp(ref input); //don't forget ref keyword.
}
public int GetIp(ref Input input)
{
input.ip.Add("192.168.119.2");
return 0;
}
此外,在 C# 中(与 Java 不同(,约定是让方法以大写字符而不是小写字符开头。查看此处以获取更多信息。