![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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 check if a C# list is empty?
Use the Any method to find whether the list is empty or not.
Set the list −
var subjects = new List<string>(); subjects.Add("Maths"); subjects.Add("Java"); subjects.Add("English"); subjects.Add("Science"); subjects.Add("Physics"); subjects.Add("Chemistry");
Now set the following condition to check whether the list is empty or not −
bool isEmpty = !subjects.Any(); if(isEmpty) { Console.WriteLine("Empty"); }else { Console.WriteLine("List is not empty"); }
The following is the complete code −
Example
using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main(string[] args) { var subjects = new List<string>(); subjects.Add("Maths"); subjects.Add("Java"); subjects.Add("English"); subjects.Add("Science"); subjects.Add("Physics"); subjects.Add("Chemistry"); foreach (var sub in subjects) { Console.WriteLine(sub); } bool isEmpty = !subjects.Any(); if(isEmpty) { Console.WriteLine("Empty"); } else { Console.WriteLine("List is not empty"); } } }
Advertisements