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

Asked

Viewed 480 times

6

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

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 within the static method Main runs on the console. I still don’t understand where the compiler "knows" is MainClass which is the main class.

This is set somewhere, or the C# Compiler understands that it is the main class because of the name MainClass?

It is possible to define another main class?

It is possible to have more than one main class?

Note: I am using Linux and compiling by Monodevelop.

1 answer

8


When there’s only one class with Main() throughout the project (where it will generate a Assembly (executable file) the compiler turns to find which one it is since there is no ambiguity. The class name matters little.

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

/main:NomeDaClass

Documentation.

In Visual Studio in Startup Object:

SS Visual Studo Startup Object

I don’t have Monodevelop, but there is somewhere to configure it. I just found how to do half manual:

SS MonoDevelop

Browser other questions tagged

You are not signed in. Login or sign up in order to post.