Are ILists passed by value?

后端 未结 6 1210
谎友^
谎友^ 2020-12-16 05:26

Passing Value Type parameters to functions in c# is by value unless you use the ref or out keyword on the parameter. But does this also apply to Reference Types?

Sp

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 06:02

    your list is passed by reference. If you want to pass a copy of the list you can do:

    IList clone = new List(list);
    

    if you add/remove elements in clone it won't modify list but the modifications of the elements themselves will be taken into account in both lists.

提交回复
热议问题