List IsNullOrEmpty C#
Ngày đăng:
05/01/2022
Trả lời:
0
Lượt xem:
118
This post will discuss how to determine whether a list is empty in C#. The solution should return true if the list contains any elements; otherwise, false. Show 1. Using Enumerable.Any() method (System.Linq)To determine whether a list contains any elements, we can use the Enumerable.Any() method. The following example demonstrates this use of Any.
DownloadRun Code 2. Using Enumerable.FirstOrDefault() method (System.Linq)The Enumerable.FirstOrDefault() method returns the first element of a sequence. If no elements are found, FirstOrDefault returns a default value. We can use this to check for an empty list as follows:
DownloadRun Code 3. Using Enumerable.Count() method (System.Linq)Another approach is to use the Enumerable.Count() method, which returns the total number of elements in a sequence.
DownloadRun Code Thats all about determining whether a list is empty in C#. |