One author of one book (sorry, I cannot give an exact quite and reference at this time), reasonably noted: "'range' is a bad term!". The set of (allowed values) is called "domain". There are two domains related to the arrays: one is the domain of its indices, most usually called 'range', and the domain of values of the elements.
If I saw only the title, I would have decided that you are talking about indices. Then you could have said some
integer index
value is in domain or not, this is simply
index >= 0 && index <= vec.length - 1
. This is almost always so, but not always. You could create not a zero-based array using the class
System.Array
, the you would need to do a bit more complicated check:
bool isInRange = index >= ver.GetLowerBound(0) && index <= vec.GetUpperBound(0);
Please see:
https://msdn.microsoft.com/en-us/library/system.array%28v=vs.110%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/system.array.getlowerbound%28v=vs.110%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/system.array.getupperbound%28v=vs.110%29.aspx[
^].
To check is some value is an element of the set of array values, you can write a function traversing all array element with
foreach
loop and compare the value for equivalence using '==' operator. Don't forget to return immediately if the matching element is found. This is too trivial to explain. But you don't have to do it, because you can use this method:
https://msdn.microsoft.com/en-us/library/eha9t187%28v=vs.110%29.aspx[
^].
You can use it with zero-based arrays anyway (which is the case with your arrays), but, generally, not with others, because "not found" is indicated by −1.
—SA