Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# use implicit void Main?

Tags:

c#

.net

standards

I don't understand why C#'s Main function is void by default (in a console project for example). In C and C++ the standard clearly says main must return int, and using a return value makes sense because we can check that return value from an external program and see if the C/C++ application finished successfully or encountered an error.

So my questions are:

  1. Why does Visual Studio declare Main as void?
  2. What's the best way of returning a value to the OS once a C# console application has finished executing?
like image 626
IVlad Avatar asked Mar 02 '10 15:03

IVlad


People also ask

Why does letter C exist?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

Does the letter C exist?

C, or c, is the third letter in the English and ISO basic Latin alphabets. Its name in English is cee (pronounced /ˈsiː/), plural cees.

Why does C make 2 sounds?

In the Latin-based orthographies of many European languages, including English, a distinction between hard and soft ⟨c⟩ occurs in which ⟨c⟩ represents two distinct phonemes. The sound of a hard ⟨c⟩ often precedes the non-front vowels ⟨a⟩, ⟨o⟩ and ⟨u⟩, and is that of the voiceless velar stop, /k/ (as in car).

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

In C#, you can use, see MSDN :

 static int Main() 
 static int Main(string[] args)
 static void Main() 
 static void Main(string[] args)

You can also return a (int) value in 2 ways.

In a Console application I would use int Main() { ...; return 2; }

In a WinForms/WPF/... app, in the rare situation it needs a return value, I would use
Environment.ExitCode = 1; or Environment.Exit(1);

like image 83
Henk Holterman Avatar answered Sep 19 '22 00:09

Henk Holterman


You can use either int or void as a return type. Thus, simply change it and return a value like in C++.

Maybe it's void by default in order not to puzzle beginners.

like image 21
Matthias Avatar answered Sep 19 '22 00:09

Matthias