There is a List <string> lst

How to check if lst [1] exists?

  • one
    What is meant by "existence"? - nitrocaster

1 answer 1

if (lst != null && lst.Count > 1 && lst[1] != null) { // код } 
  • one
    Check lst[1] not necessarily necessary: ​​it is not clear that there is "exists" in the vehicle. Check lst != null is harmful, because it does not distinguish the normal situation (the list is empty) from the non-fulfillment of the contract (lst == null). I would write this: if (lst == null) throw new ArgumentException ("appropriate description"); bool hasAtIndex1 = lst. Count> 1; - VladD