Click here to Skip to main content
Sign Up to vote bad
good
See more: C#C#4.0
hii every one ,i'm beginner
i need to know the function of keyword return in this code
 
catch (IOException exc)
       {
           Console.WriteLine(exc.Message);
           return;
       }
Posted 28 Jul '12 - 20:28


3 solutions

It does what the return keyword ALWAYS does. It ends execution in the function in question.
  Permalink  
Actually it depend on how the code continues. A return will always exit the current method (of void type in this case, equivalent of procedure in pascal for example). Placing a return somewhere can make the code of the method easier to read and understand (and less machine code to run), because without this a number of ifs has to be used. One has to be aware, that if we return from a block that created objects that has to be disposed, but are not disposed before return, will be alive until the GC reaches them. This can cause problems and even memory leaks when interacting with unmanaged code. Returning from inside the block does not conform with structured methodology (like goto), but is implemented and used often. Even goto is implemented in c# but rarely used - since it makes code harder to understand.
  Permalink  
return is a keyword which causes execution of your method to end there and then, and control to go back to the method which called it, just as if execution had reached the closing '}' of the method.
 
You can return a parameter by specifying it after the return statement.
 
For example:
        private void MyMethod(int value)
            {
            if (value < 0)
                return;
            Console.WriteLine(value);
            }
If you call MyMethod with a positive or zero parameter, it will print the value.
If you call it with a negative value it will not print anything.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 213
1 Sergey Alexandrovich Kryukov 159
2 Richard MacCutchan 150
3 Maciej Los 136
4 Tadit Dash 110
0 Sergey Alexandrovich Kryukov 10,264
1 OriginalGriff 7,917
2 CPallini 4,181
3 Rohan Leuva 3,522
4 Maciej Los 3,125


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 29 Jul 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid