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

后端 未结 5 1843
别跟我提以往
别跟我提以往 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: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.

提交回复
热议问题