List Collection对象作为方法参数 [英] List Collection object as Method Parameter

查看:449
本文介绍了List Collection对象作为方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释在调用具有列表集合作为参数的方法时如何完成内存分配。由于下面的代码片段虽然显然似乎结果相同,但它并不导致相同。
所以我想知道方法调用在内存分配方面的区别。

 使用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);
SampleMethodl(i);
Console.WriteLine(SampleMethod1的结果:+ i [0]);
SampleMethod2(i);
Console.WriteLine(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);
}
}
}


解决方案>

除非指定 ref out ,否则参数将按值传递。对于引用类型,这意味着对该对象的引用(在这种情况下, List )通过值传递。

Pass by value意味着对参数(调用语句中的表达式)求值,然后将结果值复制到参数(方法签名中列出的变量)。对于参数的任何进一步改变,在分配新值时,不是由调用者看到的。 (但继续阅读...)



这意味着在第一个方法调用中:

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

您正在创建一个新列表,将对该新列表的引用复制到 i - 但是它根本不起作用。该参数实际上只是另一个局部变量 - 对变量本身的更改不会影响调用程序。



现在将其与第二个方法进行比较:

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

这将创建一个新列表,然后立即忽略它,在 i 中传递给 j 的列表。然后,它将一个值插入列表。此方法的最终结果是将值插入到列表中。它等价于:

  public static void SampleMethod2(List< int> i)
{
i。插入(0,20);
}

请注意,这不是参数。它正在改变参数的值引用的对象。这是一个非常重要的区别。



我有一篇关于参数传递,以及参考和值类型中的另一个可能会帮助你理解这一点。


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);
        }
    }
}

解决方案

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.

这篇关于List Collection对象作为方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆