
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Dictionary Methods in C#
Dictionary is a collection of keys and values in C#. Dictionary<TKey, TValue> is included in the System.Collection.Generics namespace.
The following are the methods −
Sr.No | Method & Description |
---|---|
1 | Add Add key-value pairs in Dictionary |
2 | Clear() Remove all keys and values |
3 | Remove Removes the element with the specified key. |
4 | ContainsKey Checks whether the specified key exists in Dictionary<TKey, TValue>. |
5 | ContainsValue Checks whether the specified key value exists in Dictionary<TKey, TValue>. |
6 | Count Count the number of key-value pairs. |
7 | Clear Removes all the elements from Dictionary<TKey, TValue>. |
Let us see how to add elements into a Dictionary and display the count.
Example
using System; using System.Collections.Generic; public class Demo { public static void Main() { IDictionary<int, int> d = new Dictionary<int, int>(); d.Add(1,97); d.Add(2,89); d.Add(3,77); d.Add(4,88); d.Add(5,78); d.Add(6,98); Console.WriteLine(d.Count); } }
Output
6
- Related Articles
- Dictionary Methods in Python
- Dictionary Methods in Java
- Dictionary Methods in Python Program
- Built-in Dictionary Functions & Methods in Python
- Dictionary Methods in Python (update(), has_key(), fromkeys()
- Dictionary Methods in Python (cmp(), len(), items()…)
- Dictionary Class in C#
- Alien Dictionary in C++
- Hashtable vs. Dictionary in C#
- Case-insensitive Dictionary in C#
- How to initialize a dictionary to an empty dictionary in C#?
- Anonymous Methods in C#
- Private Methods in C#
- Extension Methods in C#
- TimeSpan.From methods in C# ()

Advertisements