C# Check empty collection
List<string> strs = null;
if(strs == null || strs.Count > 0)
Console.WriteLine("1. The list is empty.");
if(!(strs?.Count > 0))
Console.WriteLine("2. The list is empty.");
// Notice that when strs is null,
// strs?.Count will be null and "strs?.Count == 0" will be false.
if(strs is null || strs is {Count: 0})
Console.WriteLine("3. The list is empty.");
if(strs is not {Count: 0})
Console.WriteLine("4. The list is empty.");
沒有留言:
張貼留言