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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Collections Data Structure » DictionaryScreenshots 
Clone a Dictionary
        


/*
    This file is part of Sunlit World Scheme
    http://swscheme.codeplex.com/
    Copyright (c) 2010 by Edward Kiser ([email protected])

    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.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.