示例#1
0
        public static int IndexOf <T>(T[] array, T value, int startIndex, int count)
        {
            if (array == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
            }

            if ((uint)startIndex > (uint)array.Length)
            {
                ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
            }

            if ((uint)count > (uint)(array.Length - startIndex))
            {
                ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
            }

            if (RuntimeHelpers.IsBitwiseEquatable <T>())
            {
                if (Unsafe.SizeOf <T>() == sizeof(byte))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref array.GetRawSzArrayData(), startIndex),
                        Unsafe.As <T, byte>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
                else if (Unsafe.SizeOf <T>() == sizeof(char))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref Unsafe.As <byte, char>(ref array.GetRawSzArrayData()), startIndex),
                        Unsafe.As <T, char>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
                else if (Unsafe.SizeOf <T>() == sizeof(int))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref Unsafe.As <byte, int>(ref array.GetRawSzArrayData()), startIndex),
                        Unsafe.As <T, int>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
                else if (Unsafe.SizeOf <T>() == sizeof(long))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref Unsafe.As <byte, long>(ref array.GetRawSzArrayData()), startIndex),
                        Unsafe.As <T, long>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
            }

            return(EqualityComparer <T> .Default.IndexOf(array, value, startIndex, count));
        }