what is reference parameter?
what is out parameters?
what is difference these two?

Answer Posted / piyush sharma

The main difference between them is ref parameter act as
input/output whereas out parameter is simply act as output.

Let us understand with an example:

Out parameters "out" parameters are output only parameters
meaning they can only passback a value from a function.We
create a "out" parameter by preceding the parameter data
type with the out modifier. When ever a "out" parameter is
passed only an unassigned reference is passed to the
function.

using System;
class Test
{
static void out_test(out int x)
{
x=100;
}
static void Main()
{
int Myvalue=5;
MyMethod(Myvalue);
Console.WriteLine(out Myvalue);
}
}

Output of the above program would be 100 since the value of
the "out" parameter is passed back to the calling part.
Note
The modifier "out" should precede the parameter being
passed even in the calling part. "out" parameters cannot be
used within the function before assigning a value to it. A
value should be assigned to the "out" parameter before the
method returns.

Ref parameters "ref" parameters are input/output parameters
meaning they can be used for passing a value to a function
as well as to get back a value from a function.We create
a "ref" parameter by preceding the parameter data type with
the ref modifier. When ever a "ref" parameter is passed a
reference is passed to the function.

using System;
class Test
{
static void Mymethod(ref int x)
{
x=x+ 100;
}
static void Main()
{
int Myvalue=5;
MyMethod(Myvalue);
Console.WriteLine(ref Myvalue);
}
}

Output of the above program would be 105 since the "ref"
parameter acts as both input and output.
Note

The modifier "ref" should precede the parameter being
passed even in the calling part. "ref" parameters should be
assigned a value before using it to call a method.

Is This Answer Correct ?    19 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the difference between arraylist and array and in c#?

496


What is c# in asp net?

480


Which is better interface or abstract class in c#?

434


What is the difference between dataset and datatable in c#?

535


3. Use layered architecture for coding. s.no name description 1 abc xxxxxxxxx 2 abc xxxxxxxxx 3 4 5 6 7 8 Select all Clear all Add Delete Name Description Save close

1444






What is the keyword used to prevent a class from being inherited by another class?

658


How can I create image pieces/sub image?

477


How string definitions will prevent escaping on backslashes in C#?

513


What is the advantage of dependency injection?

490


Can constructor have return type c#?

628


How do I develop c# apps?

514


What is join in c#?

471


What are the differences between system.string and system.text.stringbuilder classes?

474


Is list ienumerable c#?

597


What is the use of expression tree in c#?

468