Are ILists passed by value?

后端 未结 6 1199
谎友^
谎友^ 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 05:48

    When you pass reference type by value (without ref or out keywords) you may modify this reference type inside this method and all changes will reflect to callers code.

    To solve your problem you may explicitly create a copy and pass this copy to your function, or you may use:

    list.AsReadOnly();
    

提交回复
热议问题