The Clone
method is an extension method for the Dictionary<TKey, TValue>
class. It creates a shallow copy of the specified dictionary, returning a new dictionary instance with the same key-value pairs as the original.
The Clone
method is an extension method for the Dictionary<TKey, TValue>
class. It creates a shallow copy of the specified dictionary, returning a new dictionary instance with the same key-value pairs as the original.
To use the Clone
method, call it on an existing Dictionary<TKey, TValue>
instance. This will return a new dictionary containing the same elements as the original.
// Example usage of the Clone method Dictionary<int, string> originalDict = new Dictionary<int, string> { { 1, "One" }, { 2, "Two" }, { 3, "Three" } }; // Clone the original dictionary Dictionary<int, string> clonedDict = originalDict.Clone(); // Verify that the cloned dictionary contains the same elements foreach (var kvp in clonedDict) { Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}"); }