List mylist = new List(List.UNORDERED); mylist.Add(new ListItem("apple")); mylist.Add(new ListItem("banana")); mylist.Add(new ListItem("grape")); bool containsBanana = mylist.Contains("banana"); Console.WriteLine("Does the list contain banana? " + containsBanana);
Does the list contain banana? True
public class Person { public int Id { get; set; } public string Name { get; set; } } ListpersonList = new List (); personList.Add(new Person { Id = 1, Name = "John" }); personList.Add(new Person { Id = 2, Name = "Bob" }); personList.Add(new Person { Id = 3, Name = "Alice" }); Person person = new Person { Id = 2, Name = "Bob" }; bool containsPerson = personList.Contains(person); Console.WriteLine("Does the list contain Bob? " + containsPerson);
Does the list contain Bob? TrueIn both examples, we use the iTextSharp.text.List class and its Contains() method to check whether the list contains a specific item. The first example checks whether the list contains a string while the second example checks whether the list contains a custom object.