将列表传递给方法而无需修改原始列表 [英] Pass List to method without modifying original list

查看:56
本文介绍了将列表传递给方法而无需修改原始列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是将List传递给方法并编辑该List而不修改原始List的唯一方法吗?

Is this the only way of passing a List to a method and editing that List, without modifying the original List?

class CopyTest1
{
    List<int> _myList = new List<int>();
    public CopyTest1(List<int> l)
    {
        foreach (int num in l)
        {
            _myList.Add(num);
        }
        _myList.RemoveAt(0); // no effect on original List
    }
}

推荐答案

复制列表:

_myLocalList = new List<int>(_myList);

并在本地列表上执行操作.

and perform the operations on the local list.

这篇关于将列表传递给方法而无需修改原始列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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