Mid Collections

How do you add key-value pairs to a Dictionary?

Use the Add() method or the indexer []:

// Using Add()

dictionary.Add("key1", "value1");

// Using indexer

dictionary["key2"] = "value2";

Follow:

Note: Add() throws an exception if the key already exists, while indexer will overwrite the

value.

More from C# Programming Tutorial

All questions for this course