Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I have a little confusion.
What is the meaning/purpose of return; in main() function in below example.

Sample C# code.

static void Main(string[] args){
     .... some code
     return;   // What does this mean??

}


Thanks in Advance.
Posted
Updated 13-Apr-11 9:41am
v2

This example may help you understand where it might be needed:

C#
static void Main(string[] args)
{
  // some code here
 
  if(someFlag) // exit if flag is set
  {
    return;
  }
 
  // some more code here
 
}
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 13-Apr-11 15:59pm    
That was exactly what I meant in my comment to JSOP's solution. 5+
web works 14-Apr-11 9:06am    
Thank Much All. 5 from me !
Since main() returns an int, it needs a return statement, and it should have a value after it that is of the appropriate type.

Go here:

http://msdn.microsoft.com/en-us/library/0fwzzxz2(v=vs.80).aspx[^]

BTW, in .Net 4, when you create a new console app, the return type in main() is void, so no return statement is needed. I think you can change the return type though, but I've never tried it.

Calling return; in a method that doesn't return a value doesn't serve any purpose. You can safely remove it.
 
Share this answer
 
v3
Comments
web works 13-Apr-11 15:40pm    
Opps.. Sorry i need to correct my question. Thanks
Manfred Rudolf Bihy 13-Apr-11 15:53pm    
Doesn't return; mean exactly that: To exit from a method that has void as it's return type?
Manfred Rudolf Bihy 13-Apr-11 15:55pm    
I'm always for constructing a method that only has a single point of exit, but sometimes it makes the code overly verbose and returning from several places in your code can alleviate that problem.
What do you think?
Sergey Alexandrovich Kryukov 13-Apr-11 17:44pm    
I guess, no need to check up if it's correct or not :-), changing Question does not matter; just 5.
--SA
web works 14-Apr-11 9:06am    
Thank Much All. 5 from me !
It means that the program will exit. Since main is declared as returning an int (program exit code) return should really be something like return 1; instead. This return code or program exit code can be evaluated by programs that started your executable to check if it operated correctly or if an error had caused program termination.

Main is also called a program entry point.

Hope that helps a bit.

Cheers!

-MRB
 
Share this answer
 
v2
Comments
web works 13-Apr-11 15:43pm    
I have corrected my question. It is actually void, not int !!
Manfred Rudolf Bihy 13-Apr-11 15:51pm    
Then just forget about that exit code I talked about. It merely means that the program will exit. :)
web works 13-Apr-11 15:54pm    
So, it would not return anything and just exit the code correct?
Manfred Rudolf Bihy 13-Apr-11 15:59pm    
Correct! :)
Sergey Alexandrovich Kryukov 13-Apr-11 17:43pm    
RTFM missed. He he. A 5.
--SA

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