How is an arrays Length property identified, by an internal variable (i.e. m_Length) or it’s gonna enumerate thru all the items of the array.
The difference takes place if I want to check whether an array contains any elements.
Dim asdf = { "a"c, "s"c, "d"c, "f"c }
Dim any = asdf.Any()
Dim any2 = asdf.Length > 0
(Also note that Any is an extension method, and I want to take into account the performance comparison of calling the internal get_Length vs. calling an ex. method.
The
asdfvariable in your code is not an array, it’s aString. It just so happens thatStringhas aLengthproperty and implementsIEnumerable<char>, which allows you to callAny().Nonetheless, to answer your actual question, determining an array’s length does not require enumeration; the length is stored as part of the array.
Technically, using
Lengthwould be faster than callingAny()(since that has to create an enumerator for the array, then callMoveNextonce), though the difference is likely negligible. Checking theLengthvariable is more in line with convention, though.