Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Main Method in C#
WhatsApp
Renu Agarwal
8y
6
k
0
1
25
Blog
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
Up Next
What Is Static Method And Instance Method In C#
Ebook Download
View all
Coding Principles
Read by 2.6k people
Download Now!
Learn
View all
Membership not found