tags:

views:

70

answers:

2

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;
    }
}
+6  A: 

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).

SLaks
sorry - have updated the question
Greg
@Slaks your comment re "f you want to clone the dictionary itself, you can write new Dictionary<string, long>(otherDictionary)" has set me straight - thanks
Greg
that would give you a shallow clone. I.e. clone of the collection, but not elements in it.
liho1eye
@liho1eye - both String and Int64 are immutable so there's no point in cloning. Furthermore since Int64 is a value type, any time it gets assigned to a new variable, it is "cloned" anyway.
Josh Einstein
@Josh - Oh. So in my case I'm using the Dictionary as a static variable in a backgroundWorker thread collecting data. I want to send back "snapshots" of the results periodically via "BackgroundWorker.ReportProgress". So would I be ok passing back the reference to my it?
Greg
@Greg: No, you would need to make a copy, using the code SLaks showed. Having said that, copying a dictionary is relatively expensive.
Steven Sudit
ok, so slaks code would be ok in this case (noting its a shallow clone), so no need for the code liho1eye posted - is this right?
Greg
@Greg, no you don't want to access the same dictionary instance from multiple threads. But as SLaks's example demonstrates, you can clone the dictionary by passing the other dictionary into its constructor. My point was that cloning the dictionary is enough. You don't need to clone the items in it as well because they are immutable.
Josh Einstein
thanks guys for the help
Greg
+1  A: 
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;
    }
}
liho1eye
Why not `MemberwiseClone`? (http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx)
Steven Sudit
This is not necessary at all for Dictionary<String,Int64> which is what the OP would be using it for since String and Int64 are immutable. Furthermore, such a design incorrectly implies that it supports all types of TValue but if you passed it a TValue that does not implement ICloneable, then it would fail to do what it claims. TValue should be constrained to be ICloneable but as I said, this doesn't help the OP any.
Josh Einstein
@Steven Sudit because it does shallow copy? Now it is entirely possible that OP simply misunderstood his task and shallow copy IS what he needs, but I work with what I am given.@Josh Einstein Yes, the `CloneableDictionary` does not change the fact that primitive types don't need to be cloned in the first place. I disagree with your definition of "fails" though. It attempts to do what is required, then falls back to the default behavior. That makes perfect sense to me. Yes, having `CloneableDictionary<string,long>` is completely pointless, but thats isn't the only possible use for it.
liho1eye
oh and thanks for all the minuses, I should keep that in mind when I am trying to keep my answer on top.
liho1eye
@liho: Yes, I do believe a shallow copy is sufficient, as all of the values are immutable. And please don't thank me for the downvote, as I cannot take credit for it. I do not downvote without explanation.
Steven Sudit