Is it possible to have more than one main()
method (with the same parameters) in a C# console application? If so, how?
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.
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.
We can't have more than one main() function in a project. Hint: Function can return only one value.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With