Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to initialize a dictionary to an empty dictionary in C#?
To initialize a dictionary to an empty dictionary, use the Clear() method. It clears the dictionary and forms it as empty.
dict.Clear();
After that, use the Dictionary count property to check whether the list is empty or not −
if (dict.Count == 0) {
Console.WriteLine("Dictionary is empty!");
}
Let us see the complete code −
Example
using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo {
public class Program {
public static void Main(string[] args) {
var dict = new Dictionary < string, string > ();
dict.Clear();
if (dict.Count == 0) {
Console.WriteLine("Dictionary is empty!");
}
}
}
}
Output
Dictionary is empty!
Advertisements