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

C# 在C中从静态Main传递参数#,c#,visual-studio-2012,C#,Visual Studio 2012,我是C#新手,在将字段从静态主方法传递到另一个方法时遇到问题 这是密码。我已经复制了相关行末尾的错误。使用VS2012 namespace SpaceApiTest { class SpaceApiTest { static void Main(string[] args) { Input input = new Input(); input.debug = true; // error CS1513

我是C#新手,在将字段从静态主方法传递到另一个方法时遇到问题

这是密码。我已经复制了相关行末尾的错误。使用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不同)中,约定方法以大写字符而不是小写字符开头。查看更多信息