How add key to dictionary without value?

65,624

If the value type of the dictionary is nullable, you could add a null value:

myDict.Add(key1, null);

If the value is non nullable, you can use a default value, either default or some out of range value, depending on your expected meaningful values.

myDict.Add(key1, default(int));
myDict.Add(key1, Int32.MinValue);

But

as mentioned in the comments, there is no discernible merit in doing this. You can add values at any time, there is no need to pre-initialize a dictionary with keys.

Share:
65,624
Iran_Girl
Author by

Iran_Girl

وقتی صادقی از صداقتت سواستفاده می شه people try to answer the hardest questions and don't pay attention to simple questions, although the most simple(base) questions are unanswered...!! lets don't forget, when we don't find our result after searching... we try to create new topic in forum

Updated on July 12, 2022

Comments

  • Iran_Girl
    Iran_Girl almost 2 years

    in normally we should add key and value together in dictionary type. like:

    myDict.Add(key1, value1);
    myDict.Add(key2, value2);
    

    I want to know, Is there any way to add key first, then insert its value? (not both of them at the same time)