0
public class Test
{
    public string Name;

    public void CallFunctionByObjectRef(Test a)
    {
        a.Name = "BCD";
        a = null;
    }

    public void CallFunctionByObjectRefTORef(ref Test a)
    {
        a.Name = "BCD";
        a = null;
    }
}

class Program
{
    static void Main(string[] args)
    {

        Test test = new Test();
        test.Name = "ABC";
        test.CallFunctionByObjectRef(test);


        Test test1 = new Test();
        test1.Name = "ABC";
        test1.CallFunctionByObjectRefTORef(ref test1);

        Console.WriteLine(test.Name);
        Console.WriteLine(test1.Name);
        Console.Read();
    }
}

In above called two function(using ref keyword, pass by object). I am getting different output from them. But class object by default pass by reference, why I am getting different output.

Athari
  • 33,702
  • 16
  • 105
  • 146
ABHISHEK Bandil
  • 81
  • 1
  • 10

4 Answers4

3

In your case, the reference to class object is passed by value.

Given variable a of value or reference type and method which accepts a as either value or reference:

class A {}
// or
struct A {}

var a = new A();

Foo(ref a);
// or
Foo(a);

You can:

  • Pass reference type by value: you can change object the variable refers to, you can't change the variable itself.
  • Pass reference type by reference: you can change object the variable refers to, you can change the variable itself.
  • Pass value type by value: you can't change object the variable refers to, you can't change the variable itself.
  • Pass value type by reference: you can change object the variable refers to, you can't change the variable itself.
Athari
  • 33,702
  • 16
  • 105
  • 146
1

Read Passing Reference-Type Parameters (MSDN)

A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref or out keyword.

Patrice Gahide
  • 3,644
  • 1
  • 27
  • 37
1
 /* Here you are passing pointer to the variable. For example variable test has got memory address
             * 0X200001.
             * When you pass test, you are saying that there are two variables that points to 0X200001 (one in main and another in CallFunctionByObjectRef i.e. variable a)
             * Thus when you are changing the value for a variable to null, you are changing the direction to link with null memory location.
             * 
             * It is called  reference.
             * When you came back to Main, test variable is still pointing to the memory 0X200001
            */
            Test test = new Test();
            test.Name = "ABC";

            test.CallFunctionByObjectRef(test);

            /*
            * In this case you are saying that create a variable test1 that gives a memory as: 0X200002
            * Then you are passing a pointer to pointer i.e. you are sending an actual test1 variable. 
             * so whatever you will change here will get impacted in Main.
            */
            Test test1 = new Test();
            test1.Name = "ABC";

            test1.CallFunctionByObjectRefTORef(ref test1);

            Console.WriteLine(test.Name);
            Console.WriteLine(test1.Name);
            Console.Read();

If you want to understand more then you need to get an idea from C/c++ pointer programming and search for "Reference to a Pointer"

codebased
  • 6,945
  • 9
  • 50
  • 84
0

when using the ref key word, you are passing the actual memory location to the called method, not a copy of the reference, which makes sense.

Tawfik Khalifeh
  • 939
  • 6
  • 21