Passing parameters to a method:
There are three ways that parameters can be passed
to a method:
- Pass by Value
- Pass by Reference
- Out Parameter
Pass by Value:
In
.NET Framework, all parameters are by default passed by value. When a variable
is passed as the parameter to any method, it is passed as a value. This method
copies the actual value of an argument into the formal parameter of the
function. If inside the method these values are changed or modified, the change
is not reflected in the actual passed variable. Changes made inside the
function have no effect on the actual parameter.
Passing
variable by value is useful in cases where the actual value of the variable
should not be modified by the method and the change is only limited to the
called method whereas the value of the variables in the calling method remain
unchanged.
For Example:
class
PassingValByVal
{
static void SquareIt(int x)
// The parameter x is passed by value.
// Changes to x will not affect the
original value of x.
{
x *= x;
System.Console.WriteLine("The
value inside the method: {0}", x);
}
static void Main()
{
int n = 5;
System.Console.WriteLine("The
value before calling the method: {0}", n);
SquareIt(n); // Passing the variable by value.
System.Console.WriteLine("The
value after calling the method: {0}", n);
}
}
Output:
The value before calling the method: 5
The value inside the method: 25
The
value after calling the method: 5
Pass by Reference:
C# provides a ref keyword to pass
argument as reference-type. It passes reference of arguments to the function
rather than copy of original value. When you pass parameters by reference,
unlike value parameters, a new storage location is not created for these
parameters. The method operates on the references of the variables passed in
the parameters rather than operating on their values. The changes in passed
values are permanent and modify the original variable value.
Normally, all the objects are passed
by reference as parameter to the method. On the
other hand most of the
primitive data types such as integer, double, Boolean etc. are passed by value.
For Example:
class
PassingValByRef
{
static void SquareIt(ref int x)
// The parameter x is passed by reference.
// Changes to x will affect the original
value of x.
{
x *= x;
System.Console.WriteLine("The
value inside the method: {0}", x);
}
static void Main()
{
int n = 5;
System.Console.WriteLine("The
value before calling the method: {0}", n);
SquareIt(ref n); // Passing the variable by reference.
System.Console.WriteLine("The
value after calling the method: {0}", n);
}
}
Output:
The value before calling the method: 5
The value inside the method: 25
The value after calling the method: 25
In
this example, it is not the value of n that is passed; rather, a reference to n
is passed. The parameter x is not an int; it is a reference to an int, in this
case, a reference to n. Therefore, when x is squared inside the method, what
actually is squared is what x refers to, n.
Out Parameter:
A
return statement can be used for returning only one value from a function.
However, using output parameters, you can return multiple values from a
function. Output parameters are similar to reference parameters, except that
ref requires that the variable be initialized before it is passed. Variables
passed as out arguments do not have to be initialized before being passed in a
method call. However, the called method is required to assign a value before
the method returns.
For Example:
using
System;
namespace
OutParameter
{
class Program
{
// User defined function
public void Show(out int a, out int b)
// Out parameter
{
int square = 5;
a = square;
b = square;
// Manipulating value
a *= a;
b *= b;
}
// Main function, execution entry point
of the program
static void Main(string[] args)
{
int val1 = 50, val2 = 100;
Program program = new Program(); //
Creating Object
Console.WriteLine("Value
before passing \n val1 = " + val1+" \n val2 = "+val2);
program.Show(out val1, out val2);
// Passing out argument
Console.WriteLine("Value after
passing \n val1 = " + val1 + " \n val2 = " + val2);
}
}
}
Output:
Value
before passing
val1 = 50
val2 = 100
Value
after passing
val1 = 25
val2 = 25Difference between ref & out parameter:
Prof. Shardul P. Patil
profshardulp.patil@gmail.com
No comments:
Post a Comment