.NET:字典值是通过引用还是值存储的 [英] .NET: Are Dictionary values stored by reference or value
本文介绍了.NET:字典值是通过引用还是值存储的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Dictionary<int, Product>
.如果将同一个产品添加到多个密钥中,那么是否为每个密钥存储了该对象的新实例?还是只是对原始对象的引用?
I have a Dictionary<int, Product>
. 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 reference to the original object?
此收藏非常大,每个产品都有1-10个钥匙.我主要关心的是内存分配.
This collection is very large and each product will have between 1-10 Keys to it. My primary concern is memory allocation.
推荐答案
如果Product是引用类型(类而不是struct),则仅存储引用.
If Product is a reference type (class and not struct), only a reference will be stored.
这篇关于.NET:字典值是通过引用还是值存储的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文