0

我是 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
4

1 回答 1

7

您收到该错误是因为您在方法中有一个方法。试试这个:

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 不同)中,约定是让方法以大写字符而不是小写字符开头。在这里查看更多信息。

于 2013-10-12T08:58:40.730 回答