in function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

25th Apr 2017, 3:22 PM
Jay
3 Answers
+ 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
25th Apr 2017, 3:36 PM
Rrestoring faith
Rrestoring faith - avatar
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.
26th Apr 2017, 6:59 PM
DaemonErrors
DaemonErrors - avatar
- 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.
25th Apr 2017, 9:55 PM
Valdemar
Valdemar - avatar