Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

What is the use of 'using' statement in c#?

919


What type is string in c#?

865


What is xml serialization in c#?

882


How does inheritance work in c#?

1119


Explain how do you debug an asp.net web application?

917


What is written in c#?

1014


Explain the ways to deploy an assembly?

993


Why do we Need of static class in c#?

1065


How a two-dimensional array declared in C#?

966


Why to use “using” in c#?

1071


What is data bind in c#?

913


List the two important objects of ado.net and also list the namespaces that are commonly used in ado.net to aid in connection to a database.

905


What does public mean in c#?

926


What is args c#?

944


Which are the loop types available in c#?

951