Wednesday, May 23, 2012

C# return Statement


C# return Statement

The return statement in c# will terminating execution of the current function, and transfer control to the calling function. It can be used perform conditional exit from a function to the main calling function.

protected void Page_Load(object sender, EventArgs e)
{
    printNumbers(6);
    Response.Write("Statement in the Main Calling Function");
}

void printNumbers(int nExitValue)
{
    for (int i = 1; i <= 10; i++)
    {
        if (i == nExitValue)
        {
            return;
        }
        //
        Response.Write(i.ToString() + "<br>");
    }
}
In the above example when i values is equal to 6 the return statement is reached, code execution is transferred to main calling function, the output in this case will be

1
2
3
4
5
Statement in the Main Calling Function


That’s it, we have evaluated the use of the return statement.

Related Post

Search Flipkart Products:
Flipkart.com

No comments: