Hi,
In java and c++, the main function comes out side the class but in C# the main function comes inside class. why?
Plz, Help me
Thanks.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Lalit MRank #1,186 · 1.5K reputationPosted Sep 25, 2011, 8:58 PM EDT
Prabhu RajaRank #861 · 2.6K reputationPosted Sep 25, 2011, 9:49 PM EDT
Example:
class Sample {
public static void main(String[] args) {
System.out.println("Java rules the world!");
}
}
the Above Class will Execute with out any problem.
If one Class is a Resource for another classes , that class may or may not have main method inside.
-------------------------------------------------------------
If this post helps you, then mark as "Correct Answer"
Thank You
VulpesRank #55 · 29.2K reputationPosted Sep 26, 2011, 12:09 AM EDT
Similarly, in C# the Main() method must be inside a class or struct (Java doesn't have structs).
This is because Java and C# are pure object oriented languages which don't support global functions.
However, because of its C heritage, C++ supports procedural as well as object oriented programming. Consequently it does support global functions and the main() function must in fact be defined as a global function - it can't be defined as a member function of a class.