How to pass parameters to Main() C# in visual studio for mac

后端 未结 5 1842
别跟我提以往
别跟我提以往 2021-01-18 05:59

I\'ve looked at the different resources for other editions of visual studio but it\'s not clear to me how to call Main with an arg here

using System;

namesp         


        
相关标签:
5条回答
  • 2021-01-18 06:31

    Arguments can be supplied to your application within Visual Studio for Mac by right clicking on your console application in the right hand pane then going Options > Run > Configurations > Default where you'll see an Arguments text field.

    0 讨论(0)
  • 2021-01-18 06:34

    I know this is an old post, but I think that it would be good to provide an answer for those looking to use the terminal.

    When installing Visual Studio:Community Edition on MacOS you are given a suite of tools. Including csc and mono.

    So to run a C# console program like the one below you will need to use both tools:

    using System;
    
    namespace helloWorld
    {
        class Program
        {
            static void Main(string[] args)
            {
                string name = args[0];
                Console.WriteLine("Hello {0}", name);
            }
        }
    }
    

    Then compile the C# project by running csc:

    $ csc Program.cs
    

    If there are no errors csc will then create an executable file .exe, use mono to run the executable and pass the arguments:

    $ mono Program.exe Bob
    Hello Bob
    

    With these two tools you can compile, and execute the C# program.

    0 讨论(0)
  • 2021-01-18 06:37

    As Joseph Woodward stated, you supply them in the arguments field under Project Options > Run > Configurations > Your_configuration_here.

    Note that a project rebuild is necessary for these arguments to successfully be passed into Main - otherwise args will continue to be null.

    0 讨论(0)
  • 2021-01-18 06:38

    The arguments to main will be command line parameters. If running from within visual studio, you can setup the the

    command line arguments property in the "configuration properties" of your project.

    For running it from command line, just invoke the exe file with parameters.

    Path/to/YourProg.exe arg1 arg2
    
    0 讨论(0)
  • 2021-01-18 06:44

    For some reason, the answer from @JosephWoodward isn't working for me. It looks like a Visual Studio bug, but I do have a workaround; the consequence is that you have to provide the arguments every time you want to start the app.

    1. From the menu, choose Run -> Run With -> Custom Configuration ...

    1. In the dialog, enter the arguments:

    1. If you want to debug instead of run, you need to change the Run Action at the bottom:

    0 讨论(0)
提交回复
热议问题