Check if list is empty in C#

Check if list is empty in C#

You can use Enumerable.Any:

bool isEmpty = !list.Any();
if(isEmpty)
{
    // ...
}  

If the list could be null you could use:

bool isNullOrEmpty = list?.Any() != true;

If the list implementation youre using is IEnumerable<T> and Linq is an option, you can use Any:

if (!list.Any()) {

}

Otherwise you generally have a Length or Count property on arrays and collection types respectively.

Check if list is empty in C#

    If (list.Count==0){
      //you can show your error messages here
    } else {
      //here comes your datagridview databind 
    }

You can make your datagrid visible false and make it visible on the else section.

Leave a Reply

Your email address will not be published.