Object.cs 3.2 KB

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