Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


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

Answers were Sorted based on User's Feedback



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

Answer / anna

a ref parameter must first be initialized before being
passed from the calling function to the called function.


but a out parameter need not be initialized, we can pass it
directly

Is This Answer Correct ?    52 Yes 6 No

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

Answer / lakshmi

When we pass a parameter as ref to a method, the method
refers to the same variable and changes made will affect
the actual variable. even the variable passed as out
parameter is same as ref parameter, but implementation in
c# is different,
Arguement passed as ref parameter must be initialized
before it is passed to the method. But in case of out
parameter it is not necessary. But after a call to a method
as out parameter it is necessary to initialize.
When to use out and ref parameter, out parameter is used
when we want to return more than one value from a method.

Is This Answer Correct ?    27 Yes 2 No

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

Answer / rvs varma

Ref parameter can be used as both input and o/p parameter
out parameter can be used as only output parameter

Is This Answer Correct ?    29 Yes 5 No

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

Answer / 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

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

Answer / aravazhi

normally while we are passing values to other methods it
will pass copy of the values.But we use ref or out
parameter it pass reference of values.

Is This Answer Correct ?    17 Yes 12 No

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

Answer / dharmendra nonia

A reference parameter contain only reference of variable
instead of value. In reference type parameter if changes
occurs in variable than it affect the original value. But
in out parameter it send only a copy of variable instead of
reference therefore if u change in variable no changes
occur in original value.
For example:-
class Program
{
public void refparameter(ref int a)
{
a += 5;
}
public void outparameter(out int p)
{
p = 20;
}
static void Main(string[] args)
{
Program p = new Program();
int b,c;
Console.Write("Enter a Number:");
b = Int32.Parse(Console.ReadLine());
p.refparameter(ref b);
//After the calling of refparameter it change the value of b
Console.WriteLine("After calling refparameter
method value of B is:{0}",b);
p.outparameter(out c);
Console.WriteLine("After calling the
outparameter method value of C is:{0}",c);
Console.ReadLine();
}
}

Is This Answer Correct ?    6 Yes 3 No

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

Answer / sarun p.t

out is used only to get output from a function.
it means that we cannot give any input using an out
parameter. Whatever value we assign to an out parameter
before calling to a function will be worthless, we need to
assign a value in the called function to our out parameter
before its first usage and if no usage we need to assign a
value before control leave the function.(out can be passed
to function while uninitialized)

ref parameter is used for both input and output. The only
thing is we need to assign a value before its first usage.
ref parameter must be initialized before passing to a
function.

Is This Answer Correct ?    2 Yes 2 No

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

Answer / rahul

In short ,

ref parameters needs to be initialized and can be used as
input as well as for output where as the out parameters
needs not to be initialized as there will not be any use
even if u initialize and can only be used for the the ouput
whose value to passed into the function......

for those who dont like too much reading...... it may help

Is This Answer Correct ?    1 Yes 5 No

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

Answer / vijaysaxena

out -doesnot keep initial value or assigned value.
ref-does
void f1(out int t){
t+=100 //give error becs t has no value --true for ref
t=100 //possible
}

Is This Answer Correct ?    6 Yes 11 No

Post New Answer



More C Sharp Interview Questions

What is dbml file in c#?

0 Answers  


What is a decimal in c#?

0 Answers  


what is virtual method in c#?

0 Answers  


What is thread.sleep()?

0 Answers  


Explain the security with aop?

0 Answers  






What is difference between new and override in c#?

0 Answers  


What is Fragmentation and its Types?

0 Answers   MCN Solutions,


How can we Achieve Late binding in C#.Can any give one example.

5 Answers   Value Labs,


In the page load event I assigned dropdownlist’s datasource property to a valid list. On the submit button click.. The same datasource property is coming as null. Why?

0 Answers  


What does mean c#?

0 Answers  


How do you prevent a class from being inherited in c#?

0 Answers  


What does assemblyinfo.cs consists ?

4 Answers   Microsoft,






Categories