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

本文关键字:参数传递 | 更新日期: 2023-09-27 18:22:04

如何在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";

但这失败了。

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

首先,让我们用注释来了解您的版本,然后继续了解您可能想要的内容。

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

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

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

使用公共属性,可以使用自动实现的属性以开头

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

构造函数:

public class DoSomething
{
    public DoSomething(String mystring) { ... }
    static void Main(String[] args) {
        new DoSomething(args[0]);
    }
}

编辑

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

在您提供的示例中,您正在创建的变量的作用域在Main方法中;它们不是类级别的变量。

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

我最初的代码片段是错误的;Main方法是静态的,因此不能访问实例变量。

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