简体   繁体   English

在C#中从静态Main传递参数

[英]Passing arguments from static Main in C#

I'm new to C# and having problems passing a field from the static Main method to another method. 我是C#的新手,在将字段从静态Main方法传递到另一种方法时遇到问题。

Here's the code. 这是代码。 I have copied the error at the end of the relevant line. 我已将错误复制到相关行的末尾。 Using VS2012. 使用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

You're getting that error because you have a method within a method. 之所以收到该错误,是因为方法中有一个方法。 Try this: 尝试这个:

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;    
}

Also, in C# (unlike Java), the convention is to have methods start with an uppercase character rather than lowercase. 另外,在C#(与Java不同)中,约定是使方法以大写字符而不是小写字符开头。 Take a look here for more information. 在这里查看更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM