Is there any way to programmatically determine if the value 38 is one of the elements, without iterating through each element Is there a way to do an array lookup in C#
Hi, I don't think so, but if you can, use a hashtable and then you can do what you asked for.
Edit: I was thinking in terms of accessing an object (in this case, an int) in O(1) time, that is, access it directly without any time performance problem (in constant time). Since IndexOf does the loop for you, it will take O(n) (depends on the number of elements in the array). With an hashtable the time is O(1) (normally is O(1) but not in all cases) since it saves elements in a "special" array, that uses a math function to go directly to the element you saved.
Array lookup
PaulSmithLondon
MarcD -- This is exactly what I needed!
n0n4m3 -- I did not know about Hashtables -- That will help with another part of the project I am working on!
Thanks!
mbfromit
if (Array.IndexOf(siteid, 38)!=-1)
{
// item exists
}
Hope this helps.
Brekbit
I don't think so, but if you can, use a hashtable and then you can do what you asked for.
Edit:
I was thinking in terms of accessing an object (in this case, an int) in O(1) time, that is, access it directly without any time performance problem (in constant time). Since IndexOf does the loop for you, it will take O(n) (depends on the number of elements in the array).
With an hashtable the time is O(1) (normally is O(1) but not in all cases) since it saves elements in a "special" array, that uses a math function to go directly to the element you saved.
I'm sorry if I induced you in error.