Clone a Dictionary : Dictionary « Collections Data Structure « C# / C Sharp






Clone a Dictionary

        


/*
    This file is part of Sunlit World Scheme
    http://swscheme.codeplex.com/
    Copyright (c) 2010 by Edward Kiser (edkiser@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ExprObjModel
{
    public static partial class Utils
    {

        public static Dictionary<T, U> Clone<T, U>(this Dictionary<T, U> dict)
        {
            Dictionary<T, U> dict2 = new Dictionary<T, U>();
            foreach(KeyValuePair<T, U> kvp in dict)
            {
                dict2.Add(kvp.Key, kvp.Value);
            }
            return dict2;
        }
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Dictionary(TKey, TValue) represents a collection of keys and values.
2.Try to add duplicate entry to Dictionary
3.Change value for a key
4.The indexer throws an exception if the requested key is not in the dictionary.
5.Use TryGetValue to get a value out
6.ContainsKey can be used to test keys before inserting them
7.for each KeyValuePair
8.To get the values alone, use the Values property
9.To get the keys alone, use the Keys property.
10.Use the Remove method to remove a key/value pair.
11.Dictionary List
12.Move and copy Directory
13.An Hashtable-backed dictionary that enumerates Keys and Values in insertion order.
14.Represents a dictionary which stores the values as weak references instead of strong references.
15.Dictionary Pretty Print
16.Scoped Dictionary
17.Merges two dictionaries.