Click here to Skip to main content
15,851,387 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
C#
// Namespace Declaration
using System;

// Program start class
class InteractiveWelcome
{
    // Main begins program execution.
    public static void Main()
    {
        // Write to console/get input
        Console.Write("What is your name?: ");
        Console.Write("Hello, {0}! ", Console.ReadLine());
        Console.WriteLine("Welcome to the C# Station Tutorial!");
    }
}


I was just trying to learn C# language an i noticed that unlike C++ its Main() function includes an additional class wrapped around it! So i am wondering is it necessary to define an class wrapping Main() func or declaring it without that class will too be fine!

Another thing i noticed that its Main() func consists of public static keyword , i know the meaning of public but whats the use? i mean to say that why are we declaring public as the main() function should be already available to all the code{ global scope}. I couldnt any succesful answer from any E-book, so anybody could help me try to understand them!!
Posted

The use of the access modifier public for the entry-point method Main is nothing but your fantasy. You have it just because you've written so. It doesn't have to be there.

Please see: http://en.wikipedia.org/wiki/Main_function#C.23[^].

—SA
 
Share this answer
 
Comments
Mehdi Gholam 5-Aug-13 0:25am    
You are right, main does not require to be public you can even set it to private if you want, 5'ed
Sergey Alexandrovich Kryukov 5-Aug-13 0:49am    
Thank you, Mehdi.
—SA
AbhinavGauniyal 5-Aug-13 9:13am    
saw the link , clearly understand it. Thank you very much Sir!
Sergey Alexandrovich Kryukov 5-Aug-13 10:09am    
Great. You are very welcome.
Good luck, call again.
—SA
C# is an object orientated language not a functional one hence the Main method is encapsulated within a class structure.

public static Main() is a convention for the runtime to know where to start the program from.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Aug-13 22:37pm    
Sorry, this is wrong. OP is asking about "public". And this access modifier is a bogus, it should not be there (by a pretty obvious reason). OP just caught you.
—SA
H.Brydon 4-Aug-13 22:48pm    
To be fair, OP was asking about the enclosing class as well. The answer to this is that C# is strictly object oriented, and you can't write any code unless it is in a class. As both of you point out, Main() is a special case API that gets the .entrypoint flag set on it.

+5 for both of you.
Sergey Alexandrovich Kryukov 4-Aug-13 23:28pm    
Thank you, but "public" for Main is plain wrong. As to your answer, I'm not sure it was OP's concern...
—SA
AbhinavGauniyal 5-Aug-13 9:12am    
Yeah i was asking for both. Thanks for the answer Sir, but again 1 more thing concerns me, Isn't C++ is an object oriented language , and i am never taught to wrap a class around it in my school!
H.Brydon 5-Aug-13 9:31am    
You are probably doing the right thing. C++ does not require everything to be inside a class. C# does.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900