0
in function?
Can you use "in" or contain, to check whether an int is in an array or list without using a for loop?
3 ответов
+ 5
Use indexOf()
If it returns -1 its not in the array
https://msdn.microsoft.com/en-us/library/system.string.indexof(v=vs.110).aspx
0
You can use the Contains() method. It works for both Lists and arrays. Here's an example:
int[] arr = new int[] { 1, 2, 3, 4 };
bool b = arr.Contains(3);
Console.WriteLine(b);
List<int> list = new List<int> { 1, 2, 3, 4 };
bool b = list.Contains(3);
Console.WriteLine(b);
Contains returns a boolean. True if the value is within the array/list, and false if it isn't.
- 1
And if you are proggraming C# in this decade use method called Contains (it is part of LINQ) which works on all sorts of collections.