What is the output of below code?

List<int> lst = new List<int>();
lst.Add(1);
lst.Add(2);

int index = lst.IndexOf(5);

 Posted by Rajesh_Kumar on 1/18/2014 | Category: C# Interview questions | Views: 1011 | Points: 40
Answer:

Output will be -1 .As we know that IndexOf method returns 0 if it finds the suitable match or particular value otherwise returns -1.And here List collections have 2 items i.e. 1 and 2.But 5 is not there in List.So IndexOf will search 5 inside List.Therefore it will return -1 because it will not find 5 inside List.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response