-5

I have a C# program called data-1 that I run on Mac OS

Where I run this I use:

> dotnet data-1.dll

How can I change the main so that I can enter something like

> dotnet data-1.dll 10, 20

and pass into the code the numbers 10 and 20?

static void Main(string[] args)
{
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • 2
    [Command-Line Arguments](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments) – Alexander Petrov Nov 14 '19 at 21:27

2 Answers2

2

When you pass command line arguments to your program they're contained in the args parameter of your main function. You can then access each argument through an index.

If you call for example: dotnet data1.dll 10 20 args[0] would be 10 and args[1] would be 20. Just remember that all command line arguments are initially parsed as a string so you would have to convert these string values to int or another type.

Zalhera
  • 619
  • 5
  • 18
  • I could have sworn index 0 and 1 were reserved. For stuff like the Programm name and path. But either I miss-remember, or those parameters have already been extracted and put into a property before we hit main. – Christopher Nov 14 '19 at 21:39
  • In some languages/frameworks you're correct but dotnet doesn't do that. You can get those differently. – Joelius Nov 14 '19 at 21:43
1

You can not pass integers in, only strings. That is a dos era limit on the command line and Commandline Arguments.

But parsing strings into a type like Int, is kinda the most important part of the UI work. And .NET has extensive support for it. So a simple call to Int32.Parse() will solve this.

However parse throws a vexing amount of exceptions on any parsing problem. So it is usually better to use TryParse() instead, even if the pattern for use is a bit harder to learn/more complex.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • off topic but can you elaborate on how type mapping is the most important part of UI work?? – DetectivePikachu Nov 14 '19 at 21:38
  • @DetectivePikachu I avoid using strings in the backend. Strings are for user input and output towards the user only. For those cases, they are a nessesary evil. | All the real work is done with Primitive types. You could not effeectively do math on or even store numbers in string, | Strings are arguably the 2nd worst format to process. The only thing worse is Raw Bytes. – Christopher Nov 14 '19 at 21:42
  • I see. I've always made my RPCs with string and let the backend handle type mapping and exception handling. I shy away from processing in the UI layer as much as possible. – DetectivePikachu Nov 14 '19 at 21:45
  • @DetectivePikachu: It varries by pattern. In MVVM you are encouraged to accept any string into ViewModel Properties, then to parsing (and error reporting via something like INotifyDataError) in the ViewModel classes. | But the basic idea is still the same: String are a nessesary evil of communicating with the user, not a thing you can really work with. | Also with the way and place where Parse extracts the culture to use, there can be issues requiring you to do it as close ot teh user. – Christopher Nov 14 '19 at 21:48