Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# SortedDictionary.Add() Method
The SortedDictionary.Add() method in C# is used to add an element with the specified key and value into the SortedDictionary<TKey, TValue>. Unlike regular dictionaries, SortedDictionary maintains elements in sorted order based on the keys.
Syntax
Following is the syntax for the Add() method −
public void Add(TKey key, TValue value);
Parameters
key − The key of the element to add. Cannot be null.
value − The value of the element to add. Can be null if TValue is a reference type.
Return Value
This method does not return a value. It has a void return type.
Using Add() with Integer Keys
Example
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
sortedDict.Add(100, "Mobile");
sortedDict.Add(200, "Laptop");
sortedDict.Add(300, "Desktop");
sortedDict.Add(400, "Speakers");
sortedDict.Add(500, "Headphone");
sortedDict.Add(600, "Earphone");
Console.WriteLine("SortedDictionary key-value pairs...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
The output of the above code is −
SortedDictionary key-value pairs... Key = 100, Value = Mobile Key = 200, Value = Laptop Key = 300, Value = Desktop Key = 400, Value = Speakers Key = 500, Value = Headphone Key = 600, Value = Earphone
Using Add() with String Keys
Example
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary<string, string> sortedDict = new SortedDictionary<string, string>();
sortedDict.Add("A", "John");
sortedDict.Add("B", "Andy");
sortedDict.Add("C", "Tim");
sortedDict.Add("D", "Ryan");
sortedDict.Add("E", "Kevin");
sortedDict.Add("F", "Katie");
sortedDict.Add("G", "Brad");
Console.WriteLine("SortedDictionary key-value pairs...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("\nThe SortedDictionary has the key F? = " + sortedDict.ContainsKey("F"));
}
}
The output of the above code is −
SortedDictionary key-value pairs... Key = A, Value = John Key = B, Value = Andy Key = C, Value = Tim Key = D, Value = Ryan Key = E, Value = Kevin Key = F, Value = Katie Key = G, Value = Brad The SortedDictionary has the key F? = True
Exception Handling
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary<string, int> sortedDict = new SortedDictionary<string, int>();
try {
sortedDict.Add("First", 10);
sortedDict.Add("Second", 20);
Console.WriteLine("Elements added successfully");
// This will throw ArgumentException
sortedDict.Add("First", 30);
}
catch (ArgumentException ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Elements added successfully Error: An item with the same key has already been added.
Conclusion
The SortedDictionary.Add() method adds key-value pairs while maintaining sorted order based on keys. It throws an ArgumentException if you try to add a duplicate key, making it essential to check for key existence before adding elements.
