Difference between OUT and REF parameters [Resolved]

Posted by Anu_dgr8 under C# on 9/18/2010 | Points: 10 | Views : 5380 | Status : [Member] | Replies : 3
CAN SOMEBODY TELL ME THE Difference between OUT and REF parameters with examples indicating the difference between them?

Thanks and Regards,
Hoosha Boosha



Responses

Posted by: SheoNarayan on: 9/18/2010 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
Hi Anu_dgr8,

Thanks for asking this nice question.

OUT parameter is the output value you expect from a function in which you pass the variable. In order to pass OUT parameter to the function, the variable must be defined and that variable must be assigned a new value inside the function.

REF parameter is in a way similar as OUT parameter however its not necessary to assign a new value to the variable inside the function. Look at the below code snippet. Notice that I have assigned a new value in the OutParameter function but in RefParamter function I may have opted not to assign a new value and in both case I will get a updated value of the myCount variable.

Generally OUT parameter is used when you are expecting a single value from the function, however REF is used when you have an instance of the object and you want to pass that object into multiple functions where the properties of the object needs to be changed or its method needs to be called.

int myCount = 0;


protected void Page_Load(object sender, EventArgs e)
{
OutParameter(out myCount);
Response.Write("Value after output parameter: " + myCount);

myCount = 0;
RefParameter(ref myCount);
Response.Write("<br />Value after Ref parameter: " + myCount);


myCount = 0;
SimpleParameter(myCount);
Response.Write("<br />Value after simple parameter: " + myCount);


}

void OutParameter(out int count)
{
count = 20;
}

void RefParameter(ref int count)
{
// optional, I may opt not to change this value
count = 60;
}

void SimpleParameter(int count)
{
// this will have not affect
count = 50;
}


==============
The output will be

Value after output parameter: 20
Value after Ref parameter: 60
Value after simple parameter: 0

You can also have a look at http://stackoverflow.com/questions/135234/difference-between-ref-and-out-parameters-in-net where it is explained little differently in more details.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anu_dgr8 on: 9/18/2010 [Member] Starter | Points: 25

Up
0
Down
hey Can you elaborate more on this?

Generally OUT parameter is used when you are expecting a single value from the function, however REF is used when you have an instance of the object and you want to pass that object into multiple functions where the properties of the object needs to be changed or its method needs to be called.


Thanks and Regards,
Hoosha Boosha

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: SheoNarayan on: 9/18/2010 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down

"Generally OUT parameter is used when you are expecting a single value from the function, however REF is used when you have an instance of the object and you want to pass that object into multiple functions where the properties of the object needs to be changed or its method needs to be called. "

For example (these may not be the best example, however this may give some idea):

int id = 0;

int.TryParse(Request.QuestString["id"], out id);


Here, if Request.QueryString will have id coming in on the page and if it will be a valid integer value then that value be set to id variable otherwise id will be 0.

PersonBAL bal = new PersonBAL();

ProcessPersonSalary(ref bal); // this method may access other necessary properties of the bal object and process the salary of this person and set its Salary property
FormatPersonAddress(ref bal); // this method may access other necessary properties of the bal or call a method that may give other information and format the person address.


Read the link I had provided in my previous response that may also help you.

Hope this is clear now.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response