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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9100144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:56:24  来源:igfitidea点击:

How do I pass arguments to a public class in C#?

c#.netargumentsconsole-application

提问by mmcglynn

How do I pass arguments to a public class in C#. I am new to C#, so please forgive the n00b question.

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

Given this sample class:

鉴于此示例类:

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

How do I pass the requested arguments?

如何传递请求的参数?

I would expect to write something like:

我希望写一些类似的东西:

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

But this fails.

但这失败了。

采纳答案by davioooh

The String[] argsparameter of the Mainmethod is populated when you launch the application via command line:

当您通过命令行启动应用程序时,会填充String[] argsMain方法的参数:

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

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

If you want to pass these arguments programmatically you have to set your variables as public Properties, so for example:

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

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

Then you can do:

然后你可以这样做:

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

        // do something
    }
}

回答by BrokenGlass

Use a public property, you can use an auto-implemented propertyto start with:

使用公共属性,您可以使用自动实现的属性开始:

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

回答by Daniel Mann

In the example you provided, the variables you're creating are scoped within the Mainmethod; they are not class-level variables.

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

You could access them by making them members of the class, as follows:

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

My original code snippet was wrong; your Mainmethod was static, so you can't access instance variables.

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

public class DoSomething 
{ 
    public string apple;

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

回答by Felix K.

Constructor:

构造函数:

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

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

Edit

编辑

Noticed that the C# online book is in german language. But i'm sure that there are english books too.

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

回答by CodeGnome

First, let's hit your version with notes, then move on to what you probably wanted.

首先,让我们用注释敲击您的版本,然后继续处理您可能想要的版本。

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

Here's what you probably wanted for your class definition:

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

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

}

And then you could create / manipulate the class like the following. In each case, the instance will end up with Apple = "foo" and Orange = "bar"

然后你可以创建/操作类,如下所示。在每种情况下,实例都会以 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"
};