Lets tell i’ve got 2D array of dictionaries:
public Dictionary<int, string>[,] mat = new Dictionary<int, string>[3, 5]
{
{ new Dictionary<int, string>(), new Dictionary<int, string>(), new Dictionary<int, string>() },
{ new Dictionary<int, string>(), new Dictionary<int, string>(), new Dictionary<int, string>() },
{ new Dictionary<int, string>(), new Dictionary<int, string>(), new Dictionary<int, string>() },
{ new Dictionary<int, string>(), new Dictionary<int, string>(), new Dictionary<int, string>() },
{ new Dictionary<int, string>(), new Dictionary<int, string>(), new Dictionary<int, string>() }
}
And i need to copy this to another. I tried all ways.
Something like:
matNew = mat;
But still when i change the first one, ,it automatically change the second one.
I really don’t know how to do this.
I assume you want a deep copy?
The
Dictionary(IDictionary<TKey, TValue>)constructor copies all elements from the specified dictionary, and is probably the fastest way of performing a shallow clone of an existing dictionary. This is suitable for your case, since your keys and values are both immutable (intandstring).