Empty List in C#


Set a list that has zero elements −

List<string> myList = new List<string>();

Now check whether the list is empty or null −

Console.WriteLine(myList == null);

Above, returns “False” i.e. the list is not null - the list is empty.

Let us see the complete code −

Example

 Live Demo

using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
   public static void Main() {
      List<string> myList = new List<string>();
      // returns false i.e. an empty list (not a null list)
      Console.WriteLine(myList == null);
   }
}

Output

False

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements