Click here to Skip to main content
15,887,444 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi friends,

please let me know the use of out parameter in C#.?
Posted

The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.
or
Out signifies a reference parameter. Sometimes methods must return more than one value and not store class state. Out fills these requirements. With it we pass parameters whose changes are realized in their calling methods.
Example:--
C#
using System;

class Program
{
    static void Main()
    {
    bool period; // Used as out parameter
    bool comma;
    bool semicolon;
    const string value = "has period, comma."; // Used as input string

    TestString(value, out period, out comma, out semicolon);

    Console.WriteLine(value); // Display value
    Console.Write("period: "); // Display labels and bools
    Console.WriteLine(period);
    Console.Write("comma: ");
    Console.WriteLine(comma);
    Console.Write("semicolon: ");
    Console.WriteLine(semicolon);
    }

    static void TestString(string value, out bool period, out bool comma, out bool semicolon)
    {
    period = comma = semicolon = false; // Assign all out parameters to false

    for (int i = 0; i < value.Length; i++)
    {
        switch (value[i])
        {
        case '.':
            {
            period = true; // Set out parameter
            break;
            }
        case ',':
            {
            comma = true; // Set out parameter
            break;
            }
        case ';':
            {
            semicolon = true; // Set out parameter
            break;
            }
        }
    }
    }
}


Output

has period, comma.
period: True
comma: True
semicolon: False
 
Share this answer
 
v2
Comments
leosang 5-Mar-13 7:13am    
thank you so much for ur answer... its rely helpfull for me right now
Azziet 6-Mar-13 0:33am    
most welcome !!!!
If you are familiar with Call by Value and Call by Reference, then its easy to get idea about out.
out keyword causes arguments to be passed by reference, for more kindly go to this link
http://msdn.microsoft.com/en-us/library/t3c3bfhx%28v=vs.80%29.aspx[^]
 
Share this answer
 
Comments
leosang 5-Mar-13 7:10am    
Hi hari,

really thanks for ur answer... easily i got idea about 'out' once saw ur answer.
Hari Om Prakash Sharma 5-Mar-13 23:12pm    
my pleasure sir!

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