我有一个 List,有时候它是空的或者空的。我希望能够检查它是否包含任何列表项目,如果没有,然后添加一个对象到列表。
// I have a list, sometimes it doesn't have any data added to it
var myList = new List<object>();
// Expression is always false
if (myList == null)
Console.WriteLine("List is never null");
if (myList[0] == null)
myList.Add("new item");
//Errors encountered: Index was out of range. Must be non-negative and less than the size of the collection.
// Inner Exception says "null"