public void SetValue(object?value, int index) { if (Rank != 1) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need1DArray); } var lb = GetLowerBound(0); if (index < lb || index > GetUpperBound(0)) { throw new IndexOutOfRangeException("Index has to be >= lower bound and <= upper bound of the array."); } if (GetType().GetElementType() !.IsPointer) { throw new NotSupportedException(SR.NotSupported_Type); } SetValueImpl(value, index - lb); }
public object GetValue(int index) { if (Rank != 1) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need1DArray); } var lb = GetLowerBound(0); if (index < lb || index > GetUpperBound(0)) { throw new IndexOutOfRangeException("Index has to be between upper and lower bound of the array."); } if (GetType().GetElementType().IsPointer) { throw new NotSupportedException(SR.NotSupported_Type); } return(GetValueImpl(index - lb)); }
// Converts an array of bytes into a long. public static unsafe long ToInt64(byte[] value, int startIndex) { if (value == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); } if ((uint)startIndex >= value.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } if (startIndex > value.Length - 8) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall); } fixed(byte *pbyte = &value[startIndex]) { if (startIndex % 8 == 0) // data is aligned { return(*((long *)pbyte)); } else { if (IsLittleEndian) { int i1 = (*pbyte) | (*(pbyte + 1) << 8) | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24); int i2 = (*(pbyte + 4)) | (*(pbyte + 5) << 8) | (*(pbyte + 6) << 16) | (*(pbyte + 7) << 24); return((uint)i1 | ((long)i2 << 32)); } else { int i1 = (*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3)); int i2 = (*(pbyte + 4) << 24) | (*(pbyte + 5) << 16) | (*(pbyte + 6) << 8) | (*(pbyte + 7)); return((uint)i2 | ((long)i1 << 32)); } } } }
// Create instance will create an array public static unsafe Array CreateInstance(Type elementType, int length) { if (elementType is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementType); } if (length < 0) { ThrowHelper.ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum(); } RuntimeType?t = elementType.UnderlyingSystemType as RuntimeType; if (t == null) { ThrowHelper.ThrowArgumentException( ExceptionResource.Arg_MustBeType, ExceptionArgument.elementType ); } return(InternalCreate((void *)t.TypeHandle.Value, 1, &length, null)); }
public static int BinarySearch <T>(T[] array, int index, int length, T value, System.Collections.Generic.IComparer <T>?comparer) { if (array == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if (index < 0) { ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } if (length < 0) { ThrowHelper.ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum(); } if (array.Length - index < length) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); } return(ArraySortHelper <T> .Default.BinarySearch(array, index, length, value, comparer)); }
[System.Security.SecuritySafeCritical] // auto-generated public static unsafe int ToInt32(byte[] value, int startIndex) { if (value == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); } if ((uint)startIndex >= value.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } if (startIndex > value.Length - 4) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall); } Contract.EndContractBlock(); fixed(byte *pbyte = &value[startIndex]) { if (startIndex % 4 == 0) // data is aligned { return(*((int *)pbyte)); } else { if (IsLittleEndian) { return((*pbyte) | (*(pbyte + 1) << 8) | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24)); } else { return((*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3))); } } } }
// A span-based equivalent of String.GetHashCode(StringComparison). Uses the specified comparison type. public static int GetHashCode(ReadOnlySpan <char> value, StringComparison comparisonType) { switch (comparisonType) { case StringComparison.CurrentCulture: case StringComparison.CurrentCultureIgnoreCase: return(CultureInfo.CurrentCulture.CompareInfo.GetHashCode(value, GetCaseCompareOfComparisonCulture(comparisonType))); case StringComparison.InvariantCulture: case StringComparison.InvariantCultureIgnoreCase: return(CultureInfo.InvariantCulture.CompareInfo.GetHashCode(value, GetCaseCompareOfComparisonCulture(comparisonType))); case StringComparison.Ordinal: return(GetHashCode(value)); case StringComparison.OrdinalIgnoreCase: return(GetHashCodeOrdinalIgnoreCase(value)); default: ThrowHelper.ThrowArgumentException(ExceptionResource.NotSupported_StringComparison, ExceptionArgument.comparisonType); Debug.Fail("Should not reach this point."); return(default); } }
public ArraySegment(T[] array, int offset, int count) { if (array == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if (offset < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.offset, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } if (count < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } if (array.Length - offset < count) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); } Contract.EndContractBlock(); _array = array; _offset = offset; _count = count; }
public static unsafe Array CreateInstance(Type elementType, int length1, int length2) { if (elementType is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementType); } if (length1 < 0) { ThrowHelper.ThrowArgumentOutOfRangeException( ExceptionArgument.length1, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum ); } if (length2 < 0) { ThrowHelper.ThrowArgumentOutOfRangeException( ExceptionArgument.length2, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum ); } RuntimeType?t = elementType.UnderlyingSystemType as RuntimeType; if (t == null) { ThrowHelper.ThrowArgumentException( ExceptionResource.Arg_MustBeType, ExceptionArgument.elementType ); } int *pLengths = stackalloc int[2]; pLengths[0] = length1; pLengths[1] = length2; return(InternalCreate((void *)t.TypeHandle.Value, 2, pLengths, null)); }
// Converts an array of bytes into a short. public static unsafe short ToInt16(byte[] value, int startIndex) { if (value == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); } if ((uint)startIndex >= value.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } if (startIndex > value.Length - 2) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall); } fixed(byte *pbyte = &value[startIndex]) { if (startIndex % 2 == 0) // data is aligned { return(*((short *)pbyte)); } else { if (IsLittleEndian) { return((short)((*pbyte) | (*(pbyte + 1) << 8))); } else { return((short)((*pbyte << 8) | (*(pbyte + 1)))); } } } }
// Converts an array of bytes into a String. public static string ToString(byte[] value, int startIndex, int length) { if (value == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); } if (startIndex < 0 || startIndex >= value.Length && startIndex > 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } if (length < 0) { throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_GenericPositive); } if (startIndex > value.Length - length) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall, ExceptionArgument.value); } if (length == 0) { return(string.Empty); } if (length > (int.MaxValue / 3)) { // (Int32.MaxValue / 3) == 715,827,882 Bytes == 699 MB throw new ArgumentOutOfRangeException(nameof(length), SR.Format(SR.ArgumentOutOfRange_LengthTooLarge, (int.MaxValue / 3))); } #if __MonoCS__ //use old impl for mcs const string HexValues = "0123456789ABCDEF"; int chArrayLength = length * 3; char[] chArray = new char[chArrayLength]; int i = 0; int index = startIndex; for (i = 0; i < chArrayLength; i += 3) { byte b = value[index++]; chArray[i] = HexValues[b >> 4]; chArray[i + 1] = HexValues[b & 0xF]; chArray[i + 2] = '-'; } return(new String(chArray, 0, chArray.Length - 1)); #else return(string.Create(length * 3 - 1, (value, startIndex, length), (dst, state) => { const string HexValues = "0123456789ABCDEF"; var src = new ReadOnlySpan <byte>(state.value, state.startIndex, state.length); int i = 0; int j = 0; byte b = src[i++]; dst[j++] = HexValues[b >> 4]; dst[j++] = HexValues[b & 0xF]; while (i < src.Length) { b = src[i++]; dst[j++] = '-'; dst[j++] = HexValues[b >> 4]; dst[j++] = HexValues[b & 0xF]; } })); #endif }