Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have more than one main() method in a C# program? [duplicate]

Tags:

c#

Is it possible to have more than one main() method (with the same parameters) in a C# console application? If so, how?

like image 996
Abhishek Pandey Avatar asked Jul 12 '13 05:07

Abhishek Pandey


People also ask

Can we have 2 main methods in C?

No, you cannot have more than one main() function in C language. In standard C language, the main() function is a special function that is defined as the entry point of the program.

How many main methods are there in C?

The Main Function in C Programming By default, the return type of the main function is int. There can be two types of main() functions: with and without parameters.

How many main () function we can have?

We can't have more than one main() function in a project. Hint: Function can return only one value.

How many times main () function can be invoked in a program?

Answer: 1) main() in C program is also a function. 2) Each C program must have at least one function, which is main(). 3) There is no limit on number of functions; A C program can have any number of functions.


1 Answers

You can have more than one main method, you can specify which to use as the entry point for the application by your compiler.. See this link for more detail

Example:

using System;
using System.Collections.Generic;
using System.Text;


    namespace Multiple_MainClasses
    {
        class A
        {
            static void Main(string[] args)
            {
                Console.WriteLine("I am from Class A");
                Console.ReadLine();
            }
        }
        class B
        {
            static void Main(string[] args)
            {

                Console.WriteLine("I am from Class B");
                Console.ReadLine();
            }
        }
    }

When you will run this code, you will get compilation error. To resolve go to project properties in solution explorer or press ctrl + alt + L, go to application tab and Select Class with method which you want to execute as shown below:

enter image description here

like image 80
Jainendra Avatar answered Sep 21 '22 17:09

Jainendra