我可以代替“长”,可能是可复制的,使用什么样的?
-
27-09-2019 - |
题
我可以代替“长”,可能是可复制的,有什么用?
下面参考码为哪些我在这里得到一个错误,只要是不可复制的。
public static CloneableDictionary<string, long> returnValues = new CloneableDictionary<string, long>();
编辑:我忘了提我想使用下面的代码,我发现(见下文)
public class CloneableDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TValue : ICloneable
{
public IDictionary<TKey, TValue> Clone()
{
var clone = new CloneableDictionary<TKey, TValue>();
foreach (KeyValuePair<TKey, TValue> pair in this)
{
clone.Add(pair.Key, (TValue)pair.Value.Clone());
}
return clone;
}
}
解决方案
有在克隆long
没有意义的。
您应该使用常规Dictionary<string, long>
。
如果您要克隆的字典本身,你可以写new Dictionary<string, long>(otherDictionary)
。
其他提示
public class CloneableDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
public IDictionary<TKey, TValue> Clone()
{
var clone = new CloneableDictionary<TKey, TValue>();
foreach (KeyValuePair<TKey, TValue> pair in this)
{
ICloneable clonableValue = pair.Value as ICloneable;
if (clonableValue != null)
clone.Add(pair.Key, (TValue)clonableValue.Clone());
else
clone.Add(pair.Key, pair.Value);
}
return clone;
}
}
不隶属于 StackOverflow