-1

I am trying to write a console app that has multiple methods (hopefully my terminology is correct, new to this), so I could call using 3 parameters (string, string, int) and/ or two params (string int). I was going to write two method... but Main can only have one entry point. So how can I make this work? Was wondering if anyone knew of a technique to work around this, maybe a best practice I can emulate.

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • 3
    Main has an argument array string[] args – jdweng Dec 14 '20 at 17:42
  • 1
    Does this answer your question? [How exactly is \`string\[\] args\` populated in a C# Main method?](https://stackoverflow.com/questions/12527843/how-exactly-is-string-args-populated-in-a-c-sharp-main-method) –  Dec 14 '20 at 17:46
  • You can use a command line parser like: [CommandLineParser](https://github.com/commandlineparser/commandline) or [Fluent Command Line Parser](https://github.com/fclp/fluent-command-line-parser). Thus you can get typed args in a as simple as advanced way... –  Dec 14 '20 at 17:52
  • Also [CommandLineUtils](https://github.com/natemcmaster/CommandLineUtils) that allow to directly execute commands. –  Dec 14 '20 at 18:01

2 Answers2

0

Currently, this is not possible in the way you've described it. However, there are some workarounds you could try. You can either have a single main method with a string[] for arguments and do the parsing yourself, calling the correct method depending on what arguments you receive (this is fine for very simple cases like the one you described, but not recommended for more complex cases), or you can have a library do the hard work for you.

I'd recommend trying System.CommandLine for more complicated cases, as it'll handle the parsing logic for you and just call your methods, which is probably the closest you'll get to what you're after.

0

Main method (entry point) has a parameter of type string[] (string array). You can pass parameters as many as you want. For example if you want to pass three parameters you can do it in this way C:\ConsoleApplication1 arg1 arg2 arg3

namespace ConsoleApplication1
    {
        class Program
        {    
            static void Main(string[] args)
            {    
               if(args.Length == 3)
               {
                  //process your three parameters
                  var parameter1 = args[0];
                  var parameter2 = args[1];
                  var parameter3 = args[2];
               }
               else 
               {
                  //process for example 2 parameters
               }
            }
        }
    }

For debug purposes you can define your parameters in debug tab as in image.

debug args

puko
  • 2,819
  • 4
  • 18
  • 27
  • Thanks. I knew you could not have two entry points for main. And I could think of a couple of ways to solve. I just didn't have confidence in what is a good solution, as apposed to a bad solution cause I am a newbie to C#. Thanks again for your tip. Ill use your technique. – Computermike Dec 15 '20 at 12:07