将新列表复制到此类的列表中 [英] copying the newlist to this class's list

查看:100
本文介绍了将新列表复制到此类的列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过引用didderent类中的另一个函数来发送新列表(字典),其中该列表向其中添加了一些字符串.在此类中,我还有一个名为list(字典类型)的私有变量.现在,在新列表添加一些字符串之后,我是否必须将新列表分配给私有变量列表"?还是新列表自动将此类中的列表变量作为引用传递给该列表??

I''m sending a new list(dictionary) by reference to another function in a didderent class where the list adds some strings to it . I also have a private variable called list ( dictionary type)in this class. Now do i have to asign the newlist to the private variable "list" after the newlist added some strings to it? or is the newlist autamatically filling the list varible in this class as its passed as a reference.?

Dictionary<string,> _list;

// creating a new list and to pass by reference to another method in a differerent class.
Dictionary<string,> newList = new Dictionary<string,>();
newList = null;
// passing by reference 
valid = new CaseService().ValidStageTransition(AId, BId, CId, out newList);

// after the above method returns back copy the newlist to the private variable "list"
 _list = new Dictionary<string,>(newList);





is it the right way to do?

推荐答案

如果这样做,您的原始数据将丢失.那是你想做的吗?

如果要合并两个字典,则需要将返回的集合中的内容添加到旧集合中.

[更新]
----------

为了回应您的评论,并为这些额外的类使用一些虚构的代码:

If you do that, your original data will be lost. Is that what you want to do?

If you want to merge the two dictionaries, you need to add the contents from the returned collection to the old collection.

[Update]
----------

In response to your comment, and using some imaginary code for those extra classes:

class CaseService
{
    internal bool ValidStageTransition(int p1, int p2, int p3, 
        out Dictionary<string, string> newList)
    {
        newList = new Dictionary<string, string>();
        newList.Add("aaa", "1");
        return true;
    }
}



这是您如何合并到额外集合中的方法.



Here''s how you can merge in the extra collection.

Dictionary<string, string> _list = new Dictionary<string,string>();
_list.Add("bbb", "a0");

Dictionary<string, string> newList = null;
bool valid = new CaseService().ValidStageTransition(0, 0, 0, out newList);

foreach (var item in newList)
{
    _list.Add(item.Key, item.Value);
}


因为字典是引用类型 [^ ],您不会必须通过引用"通过.只需将其传递给您的方法就足够了.

请考虑以下内容:
Since a Dictionary is a Reference Type[^] you do not have to pass it ''by reference''. Simply passing it to your Method should be enough.

Consider the following:
// Creates a new, empty dictionary.
Dictionary<string, int> dict = new Dictionary<string, int>();
// Simply pass your dictionary to a Method that adds items to it.
FillDictionary(dict);
// Your dictionary will be filled.
int i = dict.Count;


在上面的示例中,如果FillDictionary没有从字典中删除记录,则如果将dict再次传递给FillDictionary方法,您将拥有两倍的记录(如果它不尝试添加非唯一键).
如果您想清除字典,可以使用


In the above example, if FillDictionary does not remove records from your dictionary, if you pass dict to the FillDictionary Method again you will have twice as much records (if it does not try to add non-unique keys).
If you want to clear your dictionary you can use

// Clears all items from the dictionary, making it empty.
dict.Clear();


这对您意味着什么?您可以直接将_list传递给Function,所有项目都将被附加.如果您只希望使用Function中的记录,请调用_list.Clear();.然后将其传递给您的Function.
无论如何,都无需创建新字典.
希望能帮助到你! :)


What does this mean for you? You can pass _list to your Function directly and all items will be appended. If you wish to have ONLY the records from your Function call _list.Clear(); and then pass it to your Function.
In any case there is no need to create a new dictionary.
Hope it helps! :)


这篇关于将新列表复制到此类的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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