check if something is in a list c# code example
Example 1: how to check if a value is inside an array c#
string[] printer = {"jupiter", "neptune", "pangea", "mercury", "sonic"};
if(printer.Contains("jupiter"))
{
Process.Start("BLAH BLAH CODE TO ADD PRINTER VIA WINDOWS EXEC"");
}
Example 2: c# does value exist in list
public int GetItemFromList() {
List<Item> list = new List<Item>(
new Item(1),
new Item(2),
new Item(3)
);
Item testItem = new Item(1);
int index = list.FindIndex(item => testItem.Id == item.Id);
if (index > -1)
{
return index;
}
}
public class Item
{
public int Id { get; set; }
public Item() { }
public Item(int id)
{
Id = id;
}
}