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

Smi*_*thy 0 c# visual-studio-2012

我是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
Run Code Online (Sandbox Code Playgroud)

Jur*_*eri 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;    
}
Run Code Online (Sandbox Code Playgroud)

此外,在C#中(与Java不同),惯例是让方法以大写字符而不是小写字母开头.请查看此处了解更多信息.