
- 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
Get the number of key/value pairs in the StringDictionary in C#
To get the number of key/value pairs in the StringDictionary, the code is as follows −
Example
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main(){ StringDictionary strDict = new StringDictionary(); strDict.Add("1", "One"); strDict.Add("2", "Two"); strDict.Add("3", "Three"); strDict.Add("4", "Four"); Console.WriteLine("StringDictionary key-value pairs..."); IEnumerator demoEnum = strDict.GetEnumerator(); DictionaryEntry d; while (demoEnum.MoveNext()) { d = (DictionaryEntry)demoEnum.Current; Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value); } Console.WriteLine("Count of StringDictionary key-value pairs..."+strDict.Count); } }
Output
This will produce the following output −
StringDictionary key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Count of StringDictionary key-value pairs...4
Example
Let us see another example −
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main(){ StringDictionary strDict1 = new StringDictionary(); strDict1.Add("A", "John"); strDict1.Add("B", "Andy"); strDict1.Add("C", "Tim"); strDict1.Add("D", "Ryan"); strDict1.Add("E", "Kevin"); strDict1.Add("F", "Katie"); strDict1.Add("G", "Brad"); Console.WriteLine("StringDictionary1 elements..."); foreach(DictionaryEntry de in strDict1){ Console.WriteLine(de.Key + " " + de.Value); } Console.WriteLine("Count of StringDictionary1 key-value pairs..."+strDict1.Count); StringDictionary strDict2 = new StringDictionary(); strDict2.Add("1", "A"); strDict2.Add("2", "B"); strDict2.Add("3", "C"); strDict2.Add("4", "D"); strDict2.Add("5", "E"); Console.WriteLine("
StringDictionary2 key-value pairs..."); IEnumerator demoEnum = strDict2.GetEnumerator(); DictionaryEntry d; while (demoEnum.MoveNext()) { d = (DictionaryEntry)demoEnum.Current; Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value); } Console.WriteLine("Count of StringDictionary2 key-value pairs..."+strDict2.Count); } }
Output
This will produce the following output −
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad Count of StringDictionary1 key-value pairs...7 StringDictionary2 key-value pairs... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E Count of StringDictionary2 key-value pairs...5
- Related Articles
- Get the number of key/value pairs in the Dictionary in C#
- Get the number of key/value pairs contained in ListDictionary in C#
- Get or set the value associated with the specified key in StringDictionary in C#
- Count the number of key/value pairs in HybridDictionary in C#
- Count the number of key/value pairs in the Hashtable in C#
- Get the number of key/values pairs contained in OrderedDictionary in C#
- Add key and value into StringDictionary in C#
- Gets or sets the value at the specified key in StringDictionary in C#
- Display records on the basis of key-value pairs in MySQL
- Check if the StringDictionary contains a specific key in C#
- Remove entry with specified key from the StringDictionary in C#
- Get a collection of keys in the StringDictionary in C#
- Get a collection of values in the StringDictionary in C#
- How to add key/value pairs in SortedList in C#?
- Get key from value in JavaScript

Advertisements