List Collection object as Method Parameter

落爺英雄遲暮 提交于 2019-12-25 04:47:07

问题


Can anyone explain how the memory allocation is done while invoking a method having list collection as parameter. Since the code snippet below though apparently seems to result same but it is not resulting same. So I would like to know the difference in both the method call in terms of memory allocation.

using System;
using System.Collections.Generic;
namespace ListSample
{
    class ListSampleClass   
    {
        static void Main(string[] args)
        {
            List<int> i = new List<int>();
            i.Add(10);
            i.Add(15);
            SampleMethod1(i);
            Console.WriteLine("Result of SampleMethod1:"+i[0]);
            SampleMethod2(i);
            Console.WriteLine("Result of SampleMethod2:" + i[0]);
            Console.ReadKey();
        }

        public static void SampleMethod1(List<int> i)
        {
            List<int> j = new List<int>();
            j.Insert(0,20);
            i = j; 
        }

        public static void SampleMethod2(List<int> i)
        {
            List<int> j = new List<int>();            
            j = i;
            j.Insert(0, 20);
        }
    }
}

回答1:


Unless you specify ref or out, parameters are passed by value. For reference types, that means a reference to the object (the List<int> in this case) is passed by value.

"Pass by value" means that the argument (the expression in the calling statement) is evaluated, and then the resulting value is copied into the parameter (the variable listed in the method signature). Any further changes to the parameter, in terms of assigning it a new value, are not seen by the caller. (But keep reading...)

That means that in your first method call:

public static void SampleMethod1(List<int> i)
{
    List<int> j = new List<int>();
    j.Insert(0,20);
    i = j; 
}

you're creating a new list, inserting a value into it, and then copying the reference to that new list to i - but that has no effect at all. The parameter is effectively just another local variable - a change to the value of the variable itself doesn't affect the caller.

Now compare that with your second method:

public static void SampleMethod2(List<int> i)
{
    List<int> j = new List<int>();            
    j = i;
    j.Insert(0, 20);
}

This creates a new list and then immediately ignores it, instead assigning the reference to the list passed in (as i) to j. It then inserts a value into the list. The net result of this method is that a value is inserted into the list. It's equivalent to:

public static void SampleMethod2(List<int> i)
{
    i.Insert(0, 20);
}

Note that this is not changing the value of the parameter. It's making a change to the object that the value of the parameter refers to. This is a crucial difference to understand.

I have an article on parameter passing and another one on reference and value types which may help you understand this more.



来源:https://stackoverflow.com/questions/4482264/list-collection-object-as-method-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!