locked
Adding to Dictionary RRS feed

  • Question

  • User449750419 posted

    Is it possible to add one dictionary to another like for ex

    Dictionary<string,int> d1=new Dictionary<string,int>()

    {

          {"abc",1}

          {"def",2}

    };

    Dictionary<string,strng> d2=new Dictionary<string,string>()

    {

          {"str","fgh"}

          {"uyz","ghi"}

    };

    I need to add d1 to d2.Please note that both dictionary are in different signatures

    Monday, June 30, 2014 4:33 AM

Answers

  • User1293050055 posted
     class Program
        {
    
    
            static void Main(string[] args)
            {
                Dictionary<string, int> d1 = new Dictionary<string, int>()
                {
                {"abc",1},
                {"def",2}
                };
                Dictionary<string, string> d2 = new Dictionary<string, string>()
                {                   
                    {"str","fgh"},
    
                  {"uyz","ghi"}
    
                };
                foreach(var keys in d1)
                {
                    if(!d2.Keys.Contains(keys.Key))
                    {
                        d2.Add(keys.Key, keys.Value.ToString());
                    }
                }
            }
    
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 30, 2014 7:07 AM

All replies

  • User1293050055 posted
     class Program
        {
    
            
            static void Main(string[] args)
            {
                System.Collections.Generic.Dictionary<object, object> obj = new Dictionary<object, object>();
                obj.Add("abrar", 1);
                obj.Add("ab", 2);
                obj.Add("abc","adads" );
                obj.Add("abd", "add");
                foreach(var read in obj)
                {
                    Console.WriteLine("Key-->" + read.Key + ",Value--->" + read.Value);
    
                }
            }
    
        }

    Monday, June 30, 2014 5:10 AM
  • User-821857111 posted

    Please note that both dictionary are in different signatures

    Then they can't be added together as such. You can convert the contents of one to match the types of the other and add each item individually. 

    Monday, June 30, 2014 5:11 AM
  • User1293050055 posted
     class Program
        {
    
    
            static void Main(string[] args)
            {
                Dictionary<string, int> d1 = new Dictionary<string, int>()
                {
                {"abc",1},
                {"def",2}
                };
                Dictionary<string, string> d2 = new Dictionary<string, string>()
                {                   
                    {"str","fgh"},
    
                  {"uyz","ghi"}
    
                };
                foreach(var keys in d1)
                {
                    if(!d2.Keys.Contains(keys.Key))
                    {
                        d2.Add(keys.Key, keys.Value.ToString());
                    }
                }
            }
    
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 30, 2014 7:07 AM