Object.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.Versioning;
  8. namespace System
  9. {
  10. // The Object is the root class for all object in the CLR System. Object
  11. // is the super class for all other CLR objects and provide a set of methods and low level
  12. // services to subclasses. These services include object synchronization and support for clone
  13. // operations.
  14. //
  15. [Serializable]
  16. [ClassInterface(ClassInterfaceType.AutoDispatch)]
  17. [ComVisible(true)]
  18. [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  19. public partial class Object
  20. {
  21. // Creates a new instance of an Object.
  22. [NonVersionable]
  23. public Object()
  24. {
  25. }
  26. // Allow an object to free resources before the object is reclaimed by the GC.
  27. // This method's virtual slot number is hardcoded in runtimes. Do not add any virtual methods ahead of this.
  28. [NonVersionable]
  29. [SuppressMessage("Microsoft.Performance", "CA1821:RemoveEmptyFinalizers")]
  30. ~Object()
  31. {
  32. }
  33. // Returns a String which represents the object instance. The default
  34. // for an object is to return the fully qualified name of the class.
  35. public virtual string? ToString()
  36. {
  37. return GetType().ToString();
  38. }
  39. // Returns a boolean indicating if the passed in object obj is
  40. // Equal to this. Equality is defined as object equality for reference
  41. // types and bitwise equality for value types using a loader trick to
  42. // replace Equals with EqualsValue for value types).
  43. public virtual bool Equals(object? obj)
  44. {
  45. return RuntimeHelpers.Equals(this, obj);
  46. }
  47. public static bool Equals(object? objA, object? objB)
  48. {
  49. if (objA == objB)
  50. {
  51. return true;
  52. }
  53. if (objA == null || objB == null)
  54. {
  55. return false;
  56. }
  57. return objA.Equals(objB);
  58. }
  59. [NonVersionable]
  60. public static bool ReferenceEquals(object? objA, object? objB)
  61. {
  62. return objA == objB;
  63. }
  64. // GetHashCode is intended to serve as a hash function for this object.
  65. // Based on the contents of the object, the hash function will return a suitable
  66. // value with a relatively random distribution over the various inputs.
  67. //
  68. // The default implementation returns the sync block index for this instance.
  69. // Calling it on the same object multiple times will return the same value, so
  70. // it will technically meet the needs of a hash function, but it's less than ideal.
  71. // Objects (& especially value classes) should override this method.
  72. public virtual int GetHashCode()
  73. {
  74. return RuntimeHelpers.GetHashCode(this);
  75. }
  76. }
  77. }