November 21, 2024

Ever thought of using an out parameter in a method?

Lets see what it means and how much can it be useful..

Concept:

Basically, out parameter is a parameter used in a method that is even available after the method returns back to the one that called it.

In simple terms, its the way of using the “pass by reference”.

And yes, this is used whenever you wanna return multiple values from a function (we will see this how in a few mins!)

Understanding:

Whenever we mention a keyword called “out” for a method parameter, in the declaration of that method, it causes the method to refer to the same variable that was passed into the method, instead of making a new copy of that variable, i.e. the parameter that is passed as an output parameter does not create a new storage location, instead uses the same location as the variable given in the argument. — Thus, what is referred to when such a method is called is the address of that out parameter rather than its value. 🙂

Hence, any changes made to that variable that was passed in that method, are available even after the program control returns back to the calling method.

Also, there is no restriction on the number of parameters to be declared as out parameters… 🙂

Wow! This is cool yea..? So we can have multiple return values..

Enough of theory guys! Now lets jump into some code! 🙂

Example:

Just wrote a simple console application for the same. Code is as follows:

[sourcecode language=”csharp”]
static void Main(string[] args)
{
string normalstring = "Normal String";
string outstring = "Normal String";
Console.WriteLine("Before the Function Scenario: ");
Console.WriteLine("This is the normal string before the NormalMethod: " + normalstring);
Console.WriteLine("This is the out string before the OutMethod: " + outstring +"\n\n");

Console.WriteLine("In the Function Scenario: ");
NormalMethod(normalstring);
OutMethod(out outstring);

Console.WriteLine("After the Function Scenario: ");
Console.WriteLine("This is the normal string after the NormalMethod: " + normalstring);
Console.WriteLine("This is the out string after the OutMethod: " + outstring);

Console.Read();
}

static void NormalMethod(string normalstring)
{
normalstring="NORMAL STRING in Capitals";
Console.WriteLine("Normal String printed in the NormalMethod: " + normalstring);
}

static void OutMethod(out string outstring)
{
outstring = "Out String";
Console.WriteLine("Out String printed in OutMethod: " + outstring + "\n\n");
}
[/sourcecode]And the screen shot on executing this program is as follows:

Screen-shot

Something Important:

Hey but I can do the same using one more keyword called ref. Then why do I use out keyword instead of ref.

Well, there is a small reason for it. When using the ref keyword, you got to initialize the variable before you pass it on to a method. While, there is no such requirement in case of out keyword, i.e. it need not be initialize before passed to the method. (Hey this one is an advantage of out keyword over the ref keyword! 🙂 )

Also, the out keyword and the ref keyword are treated differently at Runtime rather than at Compile time. And hence, you cannot have overloaded methods wherein one takes out parameters while the other one the same takes them as ref parameters.

Conclusion:

So I think the article tells us about some very important things that may be required in programming and that can be completely fulfilled by the use of out keyword.

1. Availability of the updated value of a variable in the calling method.

2. Return multiple values from a method.

3.  No need of initializing the value of a parameter before passing it to the method.

3.  Pass by Reference Concept!

Happy Out Parameterizing! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *