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
Add the specified key and value into the ListDictionary in C#
The ListDictionary class in C# provides a simple dictionary implementation using a singly linked list. It is optimized for small collections (typically fewer than 10 items) and belongs to the System.Collections.Specialized namespace. To add key-value pairs, use the Add() method.
Syntax
Following is the syntax for adding key-value pairs to a ListDictionary −
ListDictionary.Add(object key, object value);
Parameters
key − The key to add to the ListDictionary. Cannot be null.
value − The value associated with the key. Can be null.
Using Add() Method with String Values
The following example demonstrates adding string key-value pairs to a ListDictionary −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
ListDictionary dict = new ListDictionary();
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
dict.Add("4", "Four");
dict.Add("5", "Five");
Console.WriteLine("ListDictionary key-value pairs...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
The output of the above code is −
ListDictionary key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five
Using Add() Method with Integer Values
The following example shows adding string keys with integer values and displaying only the keys −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
ListDictionary listDict = new ListDictionary();
listDict.Add("1", 100);
listDict.Add("2", 200);
listDict.Add("3", 300);
listDict.Add("4", 400);
listDict.Add("5", 500);
listDict.Add("6", 600);
listDict.Add("7", 700);
listDict.Add("8", 800);
listDict.Add("9", 900);
listDict.Add("10", 1000);
ICollection col = listDict.Keys;
Console.WriteLine("Display all the keys...");
foreach(String s in col){
Console.WriteLine(s);
}
}
}
The output of the above code is −
Display all the keys... 1 2 3 4 5 6 7 8 9 10
Using Add() Method with Mixed Data Types
ListDictionary accepts any object type as both keys and values. Here's an example with mixed data types −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
ListDictionary dict = new ListDictionary();
dict.Add("name", "John");
dict.Add("age", 25);
dict.Add("salary", 50000.50);
dict.Add("isActive", true);
Console.WriteLine("Mixed data types in ListDictionary:");
foreach(DictionaryEntry entry in dict){
Console.WriteLine("Key: {0}, Value: {1}, Type: {2}",
entry.Key, entry.Value, entry.Value.GetType().Name);
}
}
}
The output of the above code is −
Mixed data types in ListDictionary: Key: name, Value: John, Type: String Key: age, Value: 25, Type: Int32 Key: salary, Value: 50000.5, Type: Double Key: isActive, Value: True, Type: Boolean
Key Rules
Keys must be unique − adding a duplicate key throws an
ArgumentException.Keys cannot be
null, but values can benull.ListDictionary is not thread-safe for concurrent operations.
Best performance for collections with fewer than 10 items.
Conclusion
The Add() in ListDictionary allows you to insert key-value pairs into the collection. ListDictionary is ideal for small datasets due to its simple linked-list implementation, providing efficient operations when working with fewer than 10 items.
