如何将参数传递给C#中的公共类?

时间:2012-02-01 17:13:52

标签: c# .net arguments console-application

如何在C#中将参数传递给公共类。 我是C#的新手,请原谅n00b问题。

鉴于此示例类:

public class DoSomething
{
    public static void  Main(System.String[] args)
    {

        System.String apple = args[0];
        System.String orange = args[1];
        System.String banana = args[2];
        System.String peach = args[3];

        // do something
    }
}

如何传递请求的参数?

我希望写下这样的内容:

DoSomething ds = new DoSomething();
ds.apple = "pie";

但这失败了。

5 个答案:

答案 0 :(得分:6)

首先,让我们用笔记点击你的版本,然后继续你想要的。

// Here you declare your DoSomething class
public class DoSomething
{
    // now you're defining a static function called Main
    // This function isn't associated with any specific instance
    // of your class. You can invoke it just from the type,
    // like: DoSomething.Main(...)
    public static void Main(System.String[] args)
    {
        // Here, you declare some variables that are only in scope
        // during the Main function, and assign them values 
        System.String apple = args[0];
        System.String orange = args[1];
        System.String banana = args[2];
        System.String peach = args[3];
    }
        // at this point, the fruit variables are all out of scope - they
        // aren't members of your class, just variables in this function.

    // There are no variables out here in your class definition
    // There isn't a constructor for your class, so only the
    // default public one is available: DoSomething()
}

以下是您可能想要的课程定义:

public class DoSomething
{
    // The properties of the class.
    public string Apple; 
    public string Orange;

    // A constructor with no parameters
    public DoSomething()
    {
    }

    // A constructor that takes parameter to set the properties
    public DoSomething(string apple, string orange)
    {
        Apple = apple;
        Orange = orange;
    }

}

然后您可以创建/操作类,如下所示。在每种情况下,实例最终都会以Apple =“foo”和Orange =“bar”

结束
DoSomething X = new DoSomething("foo", "bar");

DoSomething Y = new DoSomething();
Y.Apple = "foo";
Y.Orange = "bar";

DoSomething Z = new DoSomething()
{
    Apple = "foo",
    Orange = "bar"
};

答案 1 :(得分:5)

通过命令行启动应用程序时,将填充String[] args方法的Main参数:

/your/application/path/DoSomething.exe arg1 arg2 arg3 ...

如果要以编程方式传递这些参数,则必须将变量设置为公共属性,例如:

public class DoSomething
{
   public string Apple { get; set; }
   public string Orange { get; set; }
   public string Banana { get; set; }
   // other fruits...
}

然后你可以这样做:

public class Test
{
    public static void  Main(System.String[] args)
    {
        DoSomething ds = new DoSomething();
        ds.Apple = "pie";

        // do something
    }
}

答案 2 :(得分:1)

使用公共财产,您可以使用auto-implemented property开头:

public class DoSomething
{
   public string Apple {get;set;}
}

答案 3 :(得分:1)

构造

public class DoSomething
{
    public DoSomething(String mystring) { ... }

    static void Main(String[] args) {
        new DoSomething(args[0]);
    }
}

修改

注意到C#在线图书是用德语写的。但我确信也有英文书籍。

答案 4 :(得分:0)

在您提供的示例中,您创建的变量的范围是Main方法;它们不是类级变量。

您可以通过将它们作为类的成员来访问它们,如下所示:

我的原始代码段错了;您的Main方法是静态的,因此您无法访问实例变量。

public class DoSomething 
{ 
    public string apple;

    public void Main(System.String[] args) 
    { 
         apple = args[0];
    } 
}