列出集合对象作为方法参数

发布于 2024-10-08 18:43:44 字数 994 浏览 6 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-10-15 18:43:44

除非您指定 refout,否则参数将按值传递。对于引用类型,这意味着对对象(本例中为 List)的引用是按值传递的。

“按值传递”意味着对参数(调用语句中的表达式)进行求值,然后将结果值复制到参数(在调用语句中列出的变量)方法签名)。调用者看不到对参数的任何进一步更改(即为其分配新值)。 (但是请继续阅读...)

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

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.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文