문제

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()
    {
        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;
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top