locked
Passing parameter to a function RRS feed

  • Question

  • Hi all,

    I had a doubt in passing a reference parameter to a function... i've read in a forum like what ever you pass to a function weather it is a value or reference type parameter then it is passed as value irrespective of the type of parameter it is passed. unless it is passed wit the ref keyword it is a value type. is that so?

    if so can u explain y the following happens

    static void Main(string[] args)
    {
    List<int> my_list = new List<int>();
    my_list.Add(10);
    my_list.Add(20);
    my_list.Add(30);
    Console.WriteLine("Befor function");
    for (int i = 0; i < my_list.Count;i++ )
    {
    Console.WriteLine("list data"+my_list[i]);
    }
    change(my_list);
    Console.WriteLine("After function");
    for (int i = 0; i < my_list.Count; i++)
    {
    Console.WriteLine("list data" + my_list[i]);
    }
    }
    public static void change(List<int> a)
    {
    List<int> new_list = new List<int>();
    a = new_list;
    }

    in this program the value of the list before and after the function does not get affected.....

    but conscider the following case

    public static void change(List<int> a)
    {
    a.Add("40");
    }

    the change function adds another value to the list that is passed to it....

    but this gets reflected in the main code that invokes the function....

    can anyone clearly explain that y is it the first case the main doesnt change and y is it the second time it is changed eventhough both the times the parameter passed to the function are reference types. if anyone can explain y it happens pleaseeeee help. thanks in advence for ur help...


    -------------kings--------------
    Wednesday, November 19, 2008 11:18 AM

Answers

  • In the first case change function sets variable a new reference, that has no relation to your variable my_list.
    In the second case function simply adds new value to my_list.
    .Net developer
    • Marked as answer by jacobkingsly Wednesday, November 19, 2008 12:30 PM
    Wednesday, November 19, 2008 12:05 PM

All replies

  • In the first case change function sets variable a new reference, that has no relation to your variable my_list.
    In the second case function simply adds new value to my_list.
    .Net developer
    • Marked as answer by jacobkingsly Wednesday, November 19, 2008 12:30 PM
    Wednesday, November 19, 2008 12:05 PM
  • thanks oleg solovey for your reply.....
    -------------kings--------------
    Wednesday, November 19, 2008 12:31 PM
  • Generic List  is always reference type , if you dont use "ref" then also it passes as reference since while sending generics you are sending address of the list.

    so in that example though you are passing as a value type it works as a reference type and add another number there
    public static void change(List<int> a)
    {
    a.Add("40");
    }




    In this case you are creating a new list and assigning to a, but in the for loop you are still using same old list.
    public static void change(List<int> a)
    {
    List<int> new_list = new List<int>();
    a = new_list;
    }
    edit: but if you change the generic list to integer or other values, you get the result of what you were expecting in question, need to pass with ref for reference

    Arjun Paudel
    Wednesday, November 19, 2008 12:45 PM
  • what if we add the a ref key word to the function parameters....
    if we use a ref keyword in both the cases the the value gets reflected....
    what magic ref keyword do.... if anyone knows please explain...
    thanks for ur help....
    -------------kings--------------
    Thursday, November 20, 2008 10:38 AM
  • Does not make any difference if you use Generic List.

    Please read my previous post...........I think you have not gone through that

    Thanks

    Arjun Paudel
    Thursday, November 20, 2008 10:46 AM
  • jacobkingsly said:

    what if we add the a ref key word to the function parameters....
    if we use a ref keyword in both the cases the the value gets reflected....
    what magic ref keyword do.... if anyone knows please explain...
    thanks for ur help....


    -------------kings--------------



    In .NET, classes are reference types and structs are value types.  When you call a method, parameters are passed by value irrespective of the type. But there are some points to understand

    • When a class object is passed, it is passed by value, but that value is a reference. You can say it is passed by reference for simplicity.
    • When a value type is passed, it is passed by value. Which means that whole value of that instance will be copied.
    When you append a ref keyword to a reference type parameter , it's reference will be passed. So the caller and the method uses same memory location. making any changes to the memory location from the method will change in the caller also. See the following code,

            static void Main(string[] args) 
            { 
                List<string> originalList = new List<string>(); 
                originalList.Add("First"); 
                // "originalList" instance is passed by value. Note this variable's value is a reference 
                PassByValue(originalList); 
                // here "originalList" will have two values 
     
                // Passing "originalList" by reference 
                PassByReference(ref originalList); 
                // originalList will be NULL here 
                Console.Read(); 
            } 
     
            static void PassByReference(ref List<string> list) 
            { 
                // list passed by reference 
                // setting a new memory location for it 
                list = null;      // This will affect the "originalList" 
            } 
     
            static void PassByValue(List<string> list) 
            { 
                // adding new item to the list 
                list.Add("new item"); 
                list = null;  // This will not affect "originalList" as "list" is a separate copy 
            } 

    I know it is confusing, but it's easy once you learn it.

    If you know C++, this is same like passing a double pointer. A pointer which contains reference to another pointer.

    Hope this helps

    *Mark the best reply as "Answered"
    Sincerly,
    Navaneeth.K.N
    Friday, November 21, 2008 5:07 AM