.NET: Are Dictionary values stored by reference or value

前端 未结 4 505
我在风中等你
我在风中等你 2021-02-07 02:14

I have a Dictionary. If the same Product is added to more than one key is an new instance of that object stored for each key? Or just a refere

4条回答
  •  攒了一身酷
    2021-02-07 03:05

    reference types are stored as references always. no one is going to guess what "clone" logic you intend for your type. if you need copies, you will have to create them on your own before placing to containers, passing to other functions and so on.

    value types are copied (simplistically byte representation copy is created, however all reference values will still be references), unless passed as ref to functions. for containers though they will be copies. unless you create a reference type wrapper for them.

提交回复
热议问题