Click here to Skip to main content
15,889,831 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi
I've been pondering on this matter for some time now...
When is it suggested to use the ref keyword in a function parameter?
I know what it does... To me it is the same as a c style pointer.
It passes the address(reference) to the variable rather than a copy of the variable.

I know that using ref is probebly less resource dependent.
But when is it actually better to rather have a function return above a ref function parameter or visa versa?

Thanks,
Posted
Updated 14-Dec-10 22:16pm
v2
Comments
Abdul Quader Mamun 15-Dec-10 4:02am    
Processor needs input.

"Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference."

This [^] should answer your queries.
 
Share this answer
 
Comments
R. Erasmus 15-Dec-10 4:49am    
what do you mean with "boxing of a value type"?
Abhinav S 15-Dec-10 6:00am    
Whenever a value type is converter to a reference type 'boxing' occurs.
R. Erasmus 15-Dec-10 6:08am    
Ok I see... Thanks!
Why would you use the 'ref' keyword with a reference type then, or is a reference type actually the equivalent of a c style: "const char*"?
It depends.

Most of the existings APIs always uses "ref" based functions and also returns value.
 
Share this answer
 
A method parameter can be modified by ref regardless of whether it is a value type or a reference type.

There is no boxing of a value type when it is passed by reference.
 
Share this answer
 
Thank you for your question.

Say you have a big method. This method performs various works step by step. You want to show progress in every stage. You will call this method by a button click like btnCalculateInterest. How you show progress to your calling method. This is not possible by return value in every stage to calling method. You have to perform all the tasks in CalculateInterest method then return. To provide this type solution you need ref.

Example:

C#
private void CalculateInterest(ref int i)
{
    //Step 1
    i=10;
    CalculateLoadAccountsInterest();
    //Step 2
    i=20;
    CalculateDepositAccountsInterest();
    //Step 3
    i=30;
    CalculateCustomerAccountsInterest();
    //Step 4
    i=40;
    CalculateFDRAccountsInterest();
    //So on..

}

protected void btnCalculateInterest_Click(object sender, EventArgs e)
{
  int val = 1;
  CalculateInterest(ref val );
  lblStatus.Text="Progress " + val.ToString() + " percentage or something" ;
}


Thanks,
Mamun
 
Share this answer
 
v4
Comments
Dalek Dave 15-Dec-10 5:35am    
Good Answer.
Abdul Quader Mamun 15-Dec-10 6:37am    
Thanks Dalek Dave!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900