public void TestObjectReferenceEquals() { Assert.That(Object.ReferenceEquals(null, null), Is.True); var suffix = "_"; var o1 = "TestString" + suffix; var o2 = "TestString" + suffix; var o3 = o1; Assert.That(Object.ReferenceEquals(null, o1), Is.False); Assert.That(Object.ReferenceEquals(o1, o2), Is.False); Assert.That(Object.ReferenceEquals(o1, o3), Is.True); // Object.ReferenceEquals(valueType1, valueType2) always returns false int age = 34; Assert.That(Object.ReferenceEquals(age, age), Is.False); // When comparing strings, the interned string is compared if the string is interned. var s1 = "TestString"; var s2 = "TestString"; Assert.That(String.IsInterned(s1), Is.Not.Null); Assert.That(String.IsInterned(s2), Is.Not.Null); Assert.That(Object.ReferenceEquals(s1, s2), Is.True); }
public static bool operator ==(DataType x, DataType y) { if (Object.ReferenceEquals(x, null) && Object.ReferenceEquals(y, null)) { return(true); } if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) { return(false); } return(x.Name.ToUpper() == y.Name.ToUpper()); }
public static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length) { if (sourceArray == null) { throw new ArgumentNullException("sourceArray"); } if (destinationArray == null) { throw new ArgumentNullException("destinationArray"); } if (length < 0) { throw new ArgumentOutOfRangeException("length", "Value has to be >= 0."); } if (sourceArray.Rank != destinationArray.Rank) { throw new RankException(SR.Rank_MultiDimNotSupported); } if (sourceIndex < 0) { throw new ArgumentOutOfRangeException("sourceIndex", "Value has to be >= 0."); } if (destinationIndex < 0) { throw new ArgumentOutOfRangeException("destinationIndex", "Value has to be >= 0."); } if (FastCopy(sourceArray, sourceIndex, destinationArray, destinationIndex, length)) { return; } int source_pos = sourceIndex - sourceArray.GetLowerBound(0); int dest_pos = destinationIndex - destinationArray.GetLowerBound(0); if (dest_pos < 0) { throw new ArgumentOutOfRangeException("destinationIndex", "Index was less than the array's lower bound in the first dimension."); } // re-ordered to avoid possible integer overflow if (source_pos > sourceArray.Length - length) { throw new ArgumentException("length"); } if (dest_pos > destinationArray.Length - length) { throw new ArgumentException("Destination array was not long enough. Check destIndex and length, and the array's lower bounds", nameof(destinationArray)); } Type src_type = sourceArray.GetType().GetElementType(); Type dst_type = destinationArray.GetType().GetElementType(); var dst_type_vt = dst_type.IsValueType; if (!Object.ReferenceEquals(sourceArray, destinationArray) || source_pos > dest_pos) { for (int i = 0; i < length; i++) { Object srcval = sourceArray.GetValueImpl(source_pos + i); if (srcval == null && dst_type_vt) { throw new InvalidCastException(); } try { destinationArray.SetValueImpl(srcval, dest_pos + i); } catch (ArgumentException) { throw CreateArrayTypeMismatchException(); } catch (InvalidCastException) { if (CanAssignArrayElement(src_type, dst_type)) { throw; } throw CreateArrayTypeMismatchException(); } } } else { for (int i = length - 1; i >= 0; i--) { Object srcval = sourceArray.GetValueImpl(source_pos + i); try { destinationArray.SetValueImpl(srcval, dest_pos + i); } catch (ArgumentException) { throw CreateArrayTypeMismatchException(); } catch { if (CanAssignArrayElement(src_type, dst_type)) { throw; } throw CreateArrayTypeMismatchException(); } } } }
private static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable) { if (sourceArray == null) { throw new ArgumentNullException(nameof(sourceArray)); } if (destinationArray == null) { throw new ArgumentNullException(nameof(destinationArray)); } if (length < 0) { throw new ArgumentOutOfRangeException(nameof(length), "Value has to be >= 0."); } if (sourceArray.Rank != destinationArray.Rank) { throw new RankException(SR.Rank_MultiDimNotSupported); } if (sourceIndex < 0) { throw new ArgumentOutOfRangeException(nameof(sourceIndex), "Value has to be >= 0."); } if (destinationIndex < 0) { throw new ArgumentOutOfRangeException(nameof(destinationIndex), "Value has to be >= 0."); } if (FastCopy(sourceArray, sourceIndex, destinationArray, destinationIndex, length)) { return; } int source_pos = sourceIndex - sourceArray.GetLowerBound(0); int dest_pos = destinationIndex - destinationArray.GetLowerBound(0); if (source_pos < 0) { throw new ArgumentOutOfRangeException(nameof(sourceIndex), "Index was less than the array's lower bound in the first dimension."); } if (dest_pos < 0) { throw new ArgumentOutOfRangeException(nameof(destinationIndex), "Index was less than the array's lower bound in the first dimension."); } // re-ordered to avoid possible integer overflow if (source_pos > sourceArray.Length - length) { throw new ArgumentException(SR.Arg_LongerThanSrcArray, nameof(sourceArray)); } if (dest_pos > destinationArray.Length - length) { throw new ArgumentException("Destination array was not long enough. Check destIndex and length, and the array's lower bounds", nameof(destinationArray)); } Type src_type = sourceArray.GetType().GetElementType() !; Type dst_type = destinationArray.GetType().GetElementType() !; var dst_type_vt = dst_type.IsValueType && Nullable.GetUnderlyingType(dst_type) == null; bool src_is_enum = src_type.IsEnum; bool dst_is_enum = dst_type.IsEnum; if (src_is_enum) { src_type = Enum.GetUnderlyingType(src_type); } if (dst_is_enum) { dst_type = Enum.GetUnderlyingType(dst_type); } if (reliable) { if (!dst_type.Equals(src_type) && !(dst_type.IsPrimitive && src_type.IsPrimitive && CanChangePrimitive(dst_type, src_type, true))) { throw new ArrayTypeMismatchException(SR.ArrayTypeMismatch_CantAssignType); } } else { if (!CanAssignArrayElement(src_type, dst_type)) { throw new ArrayTypeMismatchException(SR.ArrayTypeMismatch_CantAssignType); } } if (!Object.ReferenceEquals(sourceArray, destinationArray) || source_pos > dest_pos) { for (int i = 0; i < length; i++) { Object srcval = sourceArray.GetValueImpl(source_pos + i); if (!src_type.IsValueType && dst_is_enum) { throw new InvalidCastException(SR.InvalidCast_DownCastArrayElement); } if (dst_type_vt && (srcval == null || (src_type == typeof(object) && srcval.GetType() != dst_type))) { throw new InvalidCastException(); } try { destinationArray.SetValueRelaxedImpl(srcval, dest_pos + i); } catch (ArgumentException) { throw CreateArrayTypeMismatchException(); } } } else { for (int i = length - 1; i >= 0; i--) { Object srcval = sourceArray.GetValueImpl(source_pos + i); try { destinationArray.SetValueRelaxedImpl(srcval, dest_pos + i); } catch (ArgumentException) { throw CreateArrayTypeMismatchException(); } } } }