C# dictionary value clearing out when I clear list previously assigned to it…why?

后端 未结 6 499
北恋
北恋 2020-11-30 13:25
Dictionary > saleItemNew = new Dictionary> ();

saleItems = new List  ();
         


        
6条回答
  •  借酒劲吻你
    2020-11-30 13:58

    It uses the same reference.

    A better way to do it is

    Dictionary > saleItemNew = new Dictionary>();
    saleItemNew.Add("1", new List());
    

    And for clearing

    saleItemNew["1"] = new List(); 
    

提交回复
热议问题