
- 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
C# program to check whether a list is empty or not
Use lists in C# to store elements and fetch it. Let us see an example.
Example
using System; using System.Collections.Generic; 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); } } }
Output
Maths Java English Science Physics Chemistry
Now to check whether a list is empty or not, use the Count property.
if (subjects.Count == 0) Console.WriteLine("List is empty!");
Now let us see the complete code.
Example
using System; using System.Collections.Generic; 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"); if (subjects.Count == 0) Console.WriteLine("List is empty!"); foreach (var sub in subjects) { Console.WriteLine(sub); } } }
Output
Maths Java English Science Physics Chemistry
- Related Articles
- Python program to check whether a list is empty or not?
- Check whether a Stack is empty or not in Java
- Check whether a HashSet is empty or not in Java
- Check whether a NavigableMap empty or not in Java
- Check whether IdentityHashMap empty or not in Java?
- Java Program to check if a string is empty or not
- C# Program to Check a HashTable collection is empty or not
- Program to check whether given list is in valid state or not in Python
- Program to check whether list is alternating increase and decrease or not in Python
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to Check Whether a Number is Palindrome or Not
- C Program to Check Whether a Number is Prime or not?
- Java Program to Check Whether a Character is Alphabet or Not
- Java Program to Check Whether a Number is Prime or Not
- Haskell Program to check whether a variable is defined or not

Advertisements