开发者

What can I use in place of a "long" that could be cloneable?

What can I use in place of a "long" that could be cloneable?

Refer below to the code for which I'm getting an error here as long is not cloneable.

public static CloneableDictionary<string, long> returnValues = new CloneableDictionary<string, long>();

EDIT: I forgot to mention I was wanting to use the following code that I found (see below).

public class CloneableDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TValue : ICloneable
{
    public IDictionary<TKey, TValue> Clone()
    {
        开发者_JAVA百科var clone = new CloneableDictionary<TKey, TValue>();

        foreach (KeyValuePair<TKey, TValue> pair in this)
        {
            clone.Add(pair.Key, (TValue)pair.Value.Clone());
        }
        return clone;
    }
}


There is no point in cloning a long.

You should use a regular Dictionary<string, long>.

If you want to clone the dictionary itself, you can write 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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜