How do I pass arguments to a public class in C#.
I am new to C#, so please forgive the n00b question.
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.
The
String[] argsparameter of theMainmethod is populated when you launch the application via command line:/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:
Then you can do: