0

Out of curiosity, C# need a static Main(), some module (probably from OS) call the Main without creating object but then why this is not true for C++? Of course in C++, main is not part of any class. How these are called in 2 different ways from out side of my app?

Why C# has Main not main as in C++, is it so that C# just introduced (just for naming convention) it as method should start with capital letter or some or some other reasons?

RotatingWheel
  • 1,023
  • 14
  • 27
  • 1
    Short of the designers of C# showing up, or someone delving into their minutes (do they exist?), the reason part will be a matter of opinion. – Yakk - Adam Nevraumont Mar 15 '15 at 11:43
  • @NickyC I wonder why, a similar question got a good response in a different space-time continuum : http://stackoverflow.com/q/11332494/3317709 – Ravi M Patel Mar 15 '15 at 11:45
  • 1
    @Ravi “Good response” is highly debatable – the accepted answer is flat out wrong. Here’s a better discussion: http://programmers.stackexchange.com/q/156336/2366 (but it doesn’t really answer *this* question). – Konrad Rudolph Mar 15 '15 at 11:47
  • @KonradRudolph Agreed. By "good response" I meant, many people thought that it is a meaningful question, like the one you shared. I am not a fan of the accepted answer of the thread I shared. – Ravi M Patel Mar 15 '15 at 11:55
  • I am curious as to why people are downvoting this question. – CompuChip Mar 15 '15 at 11:56
  • @ CompuChip, probably based on my profile :-) I am sure many C++/C# developers can't answer it – RotatingWheel Mar 15 '15 at 13:54

2 Answers2

11

As far as Main vs main goes, the situation is simple: Microsoft decided on Main to comply with its own naming conventions on capitalization.

The situation with static vs. non-static requires more background, though: C++ has something that C# does not have - namely, free-standing functions (i.e. functions that are not members of any class). When you apply static keyword to a C++ function, its meaning changes based on whether the function is a member of a class or a free-standing one.

When you apply static to a member function in C++, its meaning is the same as in C#, i.e. the function becomes a class function, not an instance function.

When you apply static to a free-standing, non-member function, the meaning changes: C++ compiler makes the function invisible from outside the given translation unit. Among other things, this makes the function invisible to the part of C++ runtime library that processes the startup and passes control to your main function.

Since C++ standard specifies that the entry point is through a non-member function, applying static to main would hide it from the C++ runtime, making it unusable as an entry point. See this Q&A for more info on what happens if you make your main static.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thanks. Well explained. – RotatingWheel Mar 15 '15 at 11:58
  • Multiple entry points worth a comment? – Yakk - Adam Nevraumont Mar 15 '15 at 12:44
  • Good answer, but the first paragraph is speculation (or at least without any reference). While `Main` complies with their naming conventions, nothing would have stopped them from making the main function an exception for consistency with C, C++ and Java, and name it `main`. I could imagine that this was subject of discussion when the language was designed. – Christian Hackl Mar 15 '15 at 13:17
0

Actually the reason is very simple and one does not have to look very far to know this reason. C++ is downward compatible with C (well more or less). And C has a main function which is by convention the starting point of the application. With minor changes to C code you can compile c code with a C++ compiler.

C# however does not have this limitation and can therefore define a different entry point into the application by defining a static main as a member of an arbitrary class. Because C# does not have standalone functions and only instance methods and static methods.

Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39
  • 1
    To say that this is a “more OO way” betrays a deep misunderstanding of what OOP is. – Konrad Rudolph Mar 15 '15 at 11:52
  • Not at all. The main method belongs to a class whereas in C++ it does not. C++ is not deemed to be a pure OO language whereas java and C# are. Of course if one creates only static methods, I would not call this OO code. That is why I said a more OO way. – Philip Stuyck Mar 15 '15 at 11:59
  • 4
    Sorry but this is just fundamentally wrong. The claim that Java or C# are purely OO is a marketing lie with no basis in reality. More to the point, “just cram everything inside a class” has got nothing to do with OOP. OOP is about a protocol for organising and performing operations on data. There are plenty of *real* purely OO languages which don’t require everything to be inside a class (e.g. Ruby). – Konrad Rudolph Mar 15 '15 at 12:03
  • I am using my words very carefully, I said 'deemed' I did not say is. Fact remains that main in C++ programs is there for downward compatibility with C, and that is the only reason. I think I explained the nuance by also mentioning that an app with only static methods is not OO at all. – Philip Stuyck Mar 15 '15 at 12:05
  • @PhilipStuyck: OOP = virtual functions = choose function implementation at run-time. That's the core point of everything. Just putting some procedural code into classes with a few private fields is not enough. Function pointers in C are much closer to pure OOP than, say, the `String` class in Java. Every `utils` package or `Utils` class ever written in Java is living proof that the language is far from being "pure OOP". – Christian Hackl Mar 15 '15 at 13:13
  • It is not like I am fighting this. I know very well that OOP is about programming towards interfaces and not objects. Fact remains that the main method in C++ is there for downward compatibility with C. I did not mean to start a debate on something I agree with. I stated myself in my second comment that a program with all static methods in not OO. I agree with you that virtual is the key, because that is what makes it possible to program towards interfaces that can have a different implementation which the user of the interface does not need to know about. – Philip Stuyck Mar 15 '15 at 13:17