TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Main Method in C#
Renu Agarwal
Jun 18, 2016
5.9
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
This blog provides all the information about the Main() method defined in C#.
See following points,
Main() method is the starting point of a program.
Main() method is always "
Static
".
Main() should NOT be "
Public
". Default access specifier of Main() is Private.
Following are the acceptable signatures of Main() method,
static
void
Main(
string
[] args){}
static
void
Main(){}
staticint Main(
string
[] args){}
staticint Main(){}
Main() method can have either of the two return types - void and int.
Main() method can either have input string arguments or none.
There can be only one entry point in a program. However, we can have multiple Main() in the same project. But you need to define the "Startup object" defining which is the single entry point.
Sample Code:
We have two classes,
namespace
ConsoleApplication2
{
//Class 1
public
class
Class1
{
static
void
Main(
string
[] args)
{}
}
//Class 2
public
class
Class2
{
static
void
Main(
string
[] arg)
{}
}
}
When you compile this program, you can see both classes listed as Startup objects in Property pages of the Project.
You need to select one of the two.
C#
Main Method
Next Recommended Reading
What Is Static Method And Instance Method In C#