C# .NET - sending objects by reference Vs ref

Asked By Sandeep Bhutani on 12-Nov-08 08:57 AM

What is difference if I send an object to a function using ref keyword and without ref keyword.

e.g.

func1(myobject);

Vs

func1(ref myobject);

Solution

san san replied to Sandeep Bhutani on 12-Nov-08 09:41 AM
Hi

All parameters in C# are passed by value unless the 'out' or 'ref' keyword is used.

The confusion here is about 'passing by reference' and 'passing by value' vs. 'reference types' and 'value types'.
When passing a 'reference type' as method parameter, the reference is 'passed by value'. This means that any attempt to change the reference in the method will have no effect on the actual reference of the caller. However, if when a reference type is passed, the method may modify the actual object that is referred to.

re

Web Star replied to Sandeep Bhutani on 12-Nov-08 10:40 PM

in first u are passing  as by value where as in second u r passing by reference

Passing by Value

You pass an argument by value by specifying the http://msdn.microsoft.com/en-us/library/h2b185t2(VS.80).aspx keyword for the corresponding parameter in the procedure definition. When you use this passing mechanism, Visual Basic copies the value of the underlying programming element into a local variable in the procedure. The procedure code does not have any access to the underlying element in the calling code.

Passing by Reference

You pass an argument by reference by specifying the http://msdn.microsoft.com/en-us/library/c84t73c2(VS.80).aspx keyword for the corresponding parameter in the procedure definition. When you use this passing mechanism, Visual Basic gives the procedure a direct reference to the underlying programming element in the calling code.

Re :: Sending objects by reference vs Ref

Shailendrasinh Parmar replied to Sandeep Bhutani on 13-Nov-08 04:50 AM

In C#, parameters can be passed either by value or by reference. Passing parameters by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist. To pass a parameter by reference, use the ref or out keyword. For simplicity, only the ref keyword is used in the examples in this topic. For more information about the difference between ref and out, see ref (C# Reference), out (C# Reference), and Passing Arrays Using ref and out (C# Programming Guide). For example:

C# Copy Code
// Passing by value
static void Square(int x)
{
    // code...
}


C# Copy Code
// Passing by reference
static void Square(ref int x)
{
    // code...
}


This topic includes the following sections:

http:msdn.microsoft.com/en-us/library/9t0za53s.aspx

http:msdn.microsoft.com/en-us/library/s6938f28.aspx

Also see the follwoing article

http://en.csharp-online.net/Common_Type_System%E2%80%94Pass-by-Value_and_Pass-by-Reference

Hope it helps.

passing using ref Vs passing by reference
Sandeep Bhutani replied to Shailendrasinh Parmar on 13-Nov-08 05:13 AM

Hi,

my question is about object passing. As we know objects are passed by reference then if I use ref keyword while passing them, will that make any difference?

myfunc1(myobj);

Vs

myfunc1(ref myobj);


read this
C_A P replied to Sandeep Bhutani on 13-Nov-08 05:33 AM
Ref parameter Should be initilized before call.
Out need not to initilized before call.

Ref paramenter is nothing but in & out parameter.
Out is only out parameter.

There is no performance issue occured.

A ref or out argument must be an lvalue.ie, you need to pass the
same
signature.The casting is not allowed for both when you pass the
values to ref/out.
ex.
public void Test(ref int a, out int b)
{
a=100;
b=200;
}

public void Caller()
{
int a,b;
a=10; //If you miss init it will give
error.
Test(ref a,out b);

object x=10,y;
Test(ref (int)x,out (int)y); // It is
not Allowed.Bcs it is not lValue.
}


Parameter passing in C#
http://www.yoda.arachsys.com/csharp/parameters.html
read this link
C_A P replied to Sandeep Bhutani on 13-Nov-08 05:34 AM
http://www.dotnetspider.com/forum/155981-Difference-between-ref-out-keyword.aspx

Panu replied to C_A P on 18-Sep-10 03:01 PM
Hi.
I was thinking same problem. What is different between Func(object obj) and Func(ref object obj)? Because if I use method with out ref parameter result is same that I use method with ref parameter. Why object parameters work difference that normal varialbles like (int and bool)?