Object.cs 3.0 KB

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