- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 list to an empty list in C#?
To initialize a list to an empty list in C#, set it like the following statement without any elements −
List<string> list = new List<string>();
Now, use the Any() method to check whether the list is empty or not −
bool chk = !list.Any();
Let us see the complete code −
Example
using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { // empty list List<string> list = new List<string>(); // check for empty list bool chk = !list.Any(); if(chk) { Console.WriteLine("List is Empty!"); } else { Console.WriteLine("List isn't Empty!"); } } }
Output
List is Empty!
Advertisements