The Microsoft newly introduced out parameters in the c#.net, Using out Parameters we can send formal parameter value into the Actual parameter, while working with the out type parameters we have to initialize values to the formal parameters, out type parameters function can returns collection of values.
It’s very hard to get understand the above sentence let me explain you with example. First I will show you a code which includes a class and view variables.
class Student
{
static void Main()
{
Number abc = new Number();
int i=1000;
abc.Calc(i);
Console.WriteLine(i);
Console.Write("Press any key to continue...");
Console.ReadKey();
}
class Number
{
public void Calc(int i)
{
Console.WriteLine(i);
i = 2000;
}
}
}
OutPut
1000
1000
Here “abc” is an object that looks like “Number” class created. Variable “i” is initialized to a value 100. So using “abc” object we are calling “Calc” method with one parameter. Here i am sending value 100 when I call “Calc” method from Main() and display the value inside the “Calc” method.
Again while going back to Main() i assigned a value to “i=2000” and then WriteLine will display the value 1000 again. By this example if we change the variable name in the method “Calc” it wont effect the variable in Main() method.
So to retrieve this requirement microsoft introduce this concepts.
Here below code explains you first program using out parameter with error.
class Student
{
static void Main()
{
Number abc = new Number();
int i=1000;
abc.Calc(i);
Console.WriteLine(i);
Console.Write("Press any key to continue...");
Console.ReadKey();
}
class Number
{
public void Calc(out int i)
{
Console.WriteLine(i);
i = 2000;
}
}
}
Error
Error 1 The best overloaded method match for 'ConsoleApplication2.Student.Number.Calc(out int)' has some invalid arguments
Error 2 Argument '1' must be passed with the 'out' keyword
Error 3 Use of unassigned out parameter 'i'
The above code is same as our previous one. I just added a “out” keyword in “Calc” function. This “out” keyword indicates what ever the changes made to “i” in “Calc” function that should return to Main(). But here we forgot put “out” keyword in calling function “abc.Calc(i);” so its throwing error. We need to mention “abc.Calc(out i);” to retrieve by this error as shown below code and with little modification.
class Student
{
static void Main()
{
Number abc = new Number();
int i=1000;
Console.WriteLine(i);
abc.Calc(out i);
Console.WriteLine(i);
Console.Write("Press any key to continue...");
Console.ReadKey();
}
class Number
{
public void Calc(out int i)
{
i = 2000;
}
}
}
OutPut
1000
2000
Hence the variable “i” changed to 2000 Great. But in the previous code I am trying to display in Calc method and that through an error because when you mention “out” in the method parameters then it won’t take “in”. Like try to change “i=2000” to “i=i+2000”. It will through you an error (unassigned).
To retrieve this particular requirement Microsoft introduced Reference type parameters.
Reference type parameters:
Using reference type parameters we can send actual parameters. Address into the formal parameters, In the reference type parameters formal parameters modifications effects in the actual parameters, As per C#.Net we can get the variable address, we can send the variable address. Using “ref” keyword.
This also explained with examples. Let’s change “out” to “ref” in the above program.
class Student
{
static void Main()
{
Number abc = new Number();
int i=1000;
Console.WriteLine(i);
abc.Calc(ref i);
Console.WriteLine(i);
Console.Write("Press any key to continue...");
Console.ReadKey();
}
class Number
{
public void Calc(ref int i)
{
i =2000;
}
}
}
OutPut
1000
2000
Same as above, But as I told you we are getting an error when I am trying to use “I” in the “Calc” method like “i=i+2000”. Let’s try this using “ref” in the function.
class Student
{
static void Main()
{
Number abc = new Number();
int i = 1000;
Console.WriteLine(i);
abc.Calc(ref i);
Console.WriteLine(i);
Console.Write("Press any key to continue...");
Console.ReadKey();
}
class Number
{
public void Calc(ref int i)
{
i = i + 2000;
}
}
}
OutPut
1000
3000
Wow, it solved our error.
Let me show the drawback of “ref” parameter as shown below code.
class Student
{
static void Main()
{
Number abc = new Number();
int i;
abc.Calc(ref i); //Error
Console.WriteLine(i);
Console.Write("Press any key to continue...");
Console.ReadKey();
}
class Number
{
public void Calc(ref int i)
{
i = 1000;
i = i + 2000;
}
}
}
Error
Error 1 Use of unassigned local variable 'i'
So, by this we came to know while working on ref parameter we need to send value too. But for out parameters there is no use to send value.
C# Basic Topics
Console Application
C# Random Topics
Thursday, September 23, 2010
Subscribe to:
Posts (Atom)