Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to Solve type 'Dictionary<TKey, TValue>' requires 2 type arguments

Discussion in 'Scripting' started by Alexander21, Dec 14, 2020.

  1. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Hi All


    I am working in Dictionary. When i pass two argumens it is working> when i pass three arguments it is throw the error> how to solve it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CountryCodeJson : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     public string country;
    9.     public string IsdNo;
    10.     public string CountryCode;
    11.     void Start()
    12.     {
    13.         Pub_CountryCode();
    14.     }
    15.  
    16.     public void Pub_CountryCode(string country1)
    17.     {
    18.         IDictionary<string,string,string> values = new Dictionary<string, string,string>();
    19.  
    20.  
    21.         // Add some elements to the dictionary. There are no
    22.         // duplicate keys, but some of the values are duplicates.
    23.  
    24.  
    25.  
    26.         values.Add("India", "91", "IN");
    27.         values.Add("US", "1", "United Status");
    28.         values.Add("Algeria", "+213", "DZ");
    29.  
    30.         string result;
    31.         if (values.TryGetValue(country1, out result))
    32.         {
    33.  
    34.             //Debug.Log("the Result is" + result},
    35.             IsdNo = result;
    36.  
    37.          
    38.         }
    39.  
    40.     }
    41.  
    It shows the error Using the generic type 'Dictionary<TKey, TValue>' requires 2 type arguments

    How to pass three arguments.Two arguments is working fine. How can i solve this error.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I'm not sure that Dictionaries support anything other than a key / value pair. Are you looking for tuples perhaps?
     
  3. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    Dictionaries don´t work with 3 arguments! A dictionary represents always a key-value pair. So what you try here is not possible with Dictionaries.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    As mentioned above, Dictionaries only work in key-value pairs, so they require exactly two types - one type for the key, another type for the value.

    If you need the value type to contain more information, use a class/struct/interface as the value type.
    Code (CSharp):
    1. public class SomeClass {
    2.    string value1;
    3.    string value2;
    4. }
    Code (CSharp):
    1. Dictionary<string, SomeClass> dict = new Dictionary<string, SomeClass>();
     
    Joe-Censored, Sphinks and Bunny83 like this.
  5. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Thanks csofranz,Sphinks,Vryken for your help,. I will change code and update.