我可以使用什么代替可以克隆的"长"?

Gre*_*reg 1 c# cloneable long-integer

我可以使用什么代替可以克隆的"长"?

请参阅下面我在此处收到错误的代码,因为长期不可克隆.

public static CloneableDictionary<string, long> returnValues = new CloneableDictionary<string, long>();
Run Code Online (Sandbox Code Playgroud)

编辑:我忘了提到我想使用我找到的以下代码(见下文).

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 6

克隆没有意义long.

你应该使用常规Dictionary<string, long>.

如果要克隆字典本身,可以编写new Dictionary<string, long>(otherDictionary).

  • @ liho1eye - String和Int64都是不可变的,所以克隆没有意义.此外,由于Int64是一种值类型,因此无论何时将其分配给新变量,它都会被"克隆". (2认同)