In a console program in C #, where is the main class defined?

6

I'm doing my first console program in C #, just to do some testing.

I have two classes: HelloConsole.MainClass and HelloConsole.Calc .

I have the following code:

using System;

// Aprendendo o alias de namespace

using C = System.Console;

namespace HelloConsole
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Calc calc = new Calc (3);

            /** 
             * Agora já sei que WriteLine aceita múltiplos parâmetros
             * Tipo o Python
             * */

            C.WriteLine ("O valor é {0}", calc.value);

            C.WriteLine ("Soma de {0}", calc.sum (3));

            C.WriteLine ("Multiplo de 3 é {0}", calc.mul(3));

            Calc calc_2 = new Calc (1, 2, 3, 4, 5);

            C.WriteLine("Valores da instância diferente {0}", calc_2.value);

            C.WriteLine ("Valores de multiplicação é {0}", calc_2.mul(5));



        }
    }
}

This program works perfectly. And I understood that everything that is put inside the static method Main is executed on the console. But I still do not understand where the compiler "knows" which is MainClass which is the main class.

Is this defined somewhere, or does the C # compiler understand that it is the main class because of the name MainClass ?

Is it possible to define another main class?

Is it possible to have more than one main class?

Note : I'm using Linux and compiling for Monodevelop.

    
asked by anonymous 14.06.2016 / 18:37

1 answer

8

When you have only one class with Main() in the whole project (where you are going to generate an assembly (executable file) the compiler turns around to find out what it is since there is no ambiguity. of the class does not matter.

When you have more than one class with the method called entry point (which seems to be called the "main class" in the question, there you have to define in the compilation when it should be used with flag compilation /main

/main:NomeDaClass

Documentation .

In Visual Studio at Startup Object :

IdonothaveMonoDevelop,butthere'ssomewheretosetitup.Ijustfiguredouthowtodomanualmeans:

    
14.06.2016 / 18:42