Skip to main content
added 43 characters in body
Source Link
shA.t
  • 17k
  • 5
  • 59
  • 121

I'm reading Effective C# and there is a comment about

Object.GetHashCode()

Object.GetHashCode() that I didn't understand:

Object.GetHashCode()Object.GetHashCode() uses an internal field in in the System.ObjectSystem.Object class to generate the hash value. Each object created is is assigned a unique object key, stored as an integer, when it is created. 
These keys start at 1 and increment every time a new object of any type gets created. The object identity field is set in the System.ObjectSystem.Object constructor and cannot be modified later. Object.GetHashCode()Object.GetHashCode() returns this value as the hash hash code for a given object.

I tried to look at the documentation of Object.GetHashCode()Object.GetHashCode() and didn't find any information about this.

I wrote the simple piece of code to print the hash code of newly generated objects:

using System; namespace TestGetHashCode { class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { object o = new object(); Console.WriteLine(o.GetHashCode()); } } } } 

The first few numbers that were printed were:

37121646, 45592480, 57352375, 2637164, 41014879, 3888474, 25209742, 26966483, 31884011

37121646, 45592480, 57352375, 2637164, 41014879, 3888474, 25209742, 26966483, 31884011 

Which didn't seem to fit that

These keys start at 1 and increment every time a new object of any type gets created...bject.GetHashCode()Object.GetHashCode() returns this value

Then, in order to find this "internal field in the System.Object "System.Object" I tried using ReSharper decompileddecompiled sources but the code I found was

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] [__DynamicallyInvokable] public virtual int GetHashCode() { return RuntimeHelpers.GetHashCode(this); } 

and again using decompiled sources I found that RuntimeHelpers.GetHashCodeRuntimeHelpers.GetHashCode was implemented as

[SecuritySafeCritical] [__DynamicallyInvokable] [MethodImpl(MethodImplOptions.InternalCall)] public static int GetHashCode(object o); 

following the MethodImpl attribute it seems that I can't view the implementation and this is a dead end for me.

Can someone please explain the comment by the author (the first quote) ?

What is the internal field within the Object class and how it is used for the implementation of the Object.GetHashCode() Object.GetHashCode()?

I'm reading Effective C# and there is a comment about

Object.GetHashCode()

that I didn't understand:

Object.GetHashCode() uses an internal field in the System.Object class to generate the hash value. Each object created is assigned a unique object key, stored as an integer, when it is created. These keys start at 1 and increment every time a new object of any type gets created. The object identity field is set in the System.Object constructor and cannot be modified later. Object.GetHashCode() returns this value as the hash code for a given object.

I tried to look at the documentation of Object.GetHashCode() and didn't find any information about this.

I wrote the simple piece of code to print the hash code of newly generated objects:

using System; namespace TestGetHashCode { class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { object o = new object(); Console.WriteLine(o.GetHashCode()); } } } } 

The first few numbers that were printed were:

37121646, 45592480, 57352375, 2637164, 41014879, 3888474, 25209742, 26966483, 31884011

Which didn't seem to fit that

These keys start at 1 and increment every time a new object of any type gets created...bject.GetHashCode() returns this value

Then, in order to find this "internal field in the System.Object " I tried using ReSharper decompiled sources but the code I found was

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] [__DynamicallyInvokable] public virtual int GetHashCode() { return RuntimeHelpers.GetHashCode(this); } 

and again using decompiled sources I found that RuntimeHelpers.GetHashCode was implemented as

[SecuritySafeCritical] [__DynamicallyInvokable] [MethodImpl(MethodImplOptions.InternalCall)] public static int GetHashCode(object o); 

following the MethodImpl attribute it seems that I can't view the implementation and this is a dead end for me.

Can someone please explain the comment by the author (the first quote) ?

What is the internal field within the Object class and how it is used for the implementation of the Object.GetHashCode() ?

I'm reading Effective C# and there is a comment about Object.GetHashCode() that I didn't understand:

Object.GetHashCode() uses an internal field in the System.Object class to generate the hash value. Each object created is assigned a unique object key, stored as an integer, when it is created. 
These keys start at 1 and increment every time a new object of any type gets created. The object identity field is set in the System.Object constructor and cannot be modified later. Object.GetHashCode() returns this value as the hash code for a given object.

I tried to look at the documentation of Object.GetHashCode() and didn't find any information about this.

I wrote the simple piece of code to print the hash code of newly generated objects:

using System; namespace TestGetHashCode { class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { object o = new object(); Console.WriteLine(o.GetHashCode()); } } } } 

The first few numbers that were printed were:

37121646, 45592480, 57352375, 2637164, 41014879, 3888474, 25209742, 26966483, 31884011 

Which didn't seem to fit that

These keys start at 1 and increment every time a new object of any type gets created...Object.GetHashCode() returns this value

Then, in order to find this "internal field in the System.Object" I tried using ReSharper decompiled sources but the code I found was

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] [__DynamicallyInvokable] public virtual int GetHashCode() { return RuntimeHelpers.GetHashCode(this); } 

and again using decompiled sources I found that RuntimeHelpers.GetHashCode was implemented as

[SecuritySafeCritical] [__DynamicallyInvokable] [MethodImpl(MethodImplOptions.InternalCall)] public static int GetHashCode(object o); 

following the MethodImpl attribute it seems that I can't view the implementation and this is a dead end for me.

Can someone please explain the comment by the author (the first quote) ?

What is the internal field within the Object class and how it is used for the implementation of the Object.GetHashCode()?

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I'm reading Effective C# and there is a comment about

Object.GetHashCode()

that I didn't understand:

Object.GetHashCode() uses an internal field in the System.Object class to generate the hash value. Each object created is assigned a unique object key, stored as an integer, when it is created. These keys start at 1 and increment every time a new object of any type gets created. The object identity field is set in the System.Object constructor and cannot be modified later. Object.GetHashCode() returns this value as the hash code for a given object.

I tried to look at the documentation of Object.GetHashCode() and didn't find any information about this.

I wrote the simple piece of code to print the hash code of newly generated objects:

using System; namespace TestGetHashCode { class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { object o = new object(); Console.WriteLine(o.GetHashCode()); } } } } 

The first few numbers that were printed were:

37121646, 45592480, 57352375, 2637164, 41014879, 3888474, 25209742, 26966483, 31884011

Which didn't seem to fit that

These keys start at 1 and increment every time a new object of any type gets created...bject.GetHashCode() returns this value

Then, in order to find this "internal field in the System.Object " I tried using ReSharper decompiled sources but the code I found was

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] [__DynamicallyInvokable] public virtual int GetHashCode() { return RuntimeHelpers.GetHashCode(this); } 

and again using decompiled sources I found that RuntimeHelpers.GetHashCode was implemented as

[SecuritySafeCritical] [__DynamicallyInvokable] [MethodImpl(MethodImplOptions.InternalCall)] public static int GetHashCode(object o); 

following the MethodImpl attributethe MethodImpl attribute it seems that I can't view the implementation and this is a dead end for me.

Can someone please explain the comment by the author (the first quote) ?

What is the internal field within the Object class and how it is used for the implementation of the Object.GetHashCode() ?

I'm reading Effective C# and there is a comment about

Object.GetHashCode()

that I didn't understand:

Object.GetHashCode() uses an internal field in the System.Object class to generate the hash value. Each object created is assigned a unique object key, stored as an integer, when it is created. These keys start at 1 and increment every time a new object of any type gets created. The object identity field is set in the System.Object constructor and cannot be modified later. Object.GetHashCode() returns this value as the hash code for a given object.

I tried to look at the documentation of Object.GetHashCode() and didn't find any information about this.

I wrote the simple piece of code to print the hash code of newly generated objects:

using System; namespace TestGetHashCode { class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { object o = new object(); Console.WriteLine(o.GetHashCode()); } } } } 

The first few numbers that were printed were:

37121646, 45592480, 57352375, 2637164, 41014879, 3888474, 25209742, 26966483, 31884011

Which didn't seem to fit that

These keys start at 1 and increment every time a new object of any type gets created...bject.GetHashCode() returns this value

Then, in order to find this "internal field in the System.Object " I tried using ReSharper decompiled sources but the code I found was

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] [__DynamicallyInvokable] public virtual int GetHashCode() { return RuntimeHelpers.GetHashCode(this); } 

and again using decompiled sources I found that RuntimeHelpers.GetHashCode was implemented as

[SecuritySafeCritical] [__DynamicallyInvokable] [MethodImpl(MethodImplOptions.InternalCall)] public static int GetHashCode(object o); 

following the MethodImpl attribute it seems that I can't view the implementation and this is a dead end for me.

Can someone please explain the comment by the author (the first quote) ?

What is the internal field within the Object class and how it is used for the implementation of the Object.GetHashCode() ?

I'm reading Effective C# and there is a comment about

Object.GetHashCode()

that I didn't understand:

Object.GetHashCode() uses an internal field in the System.Object class to generate the hash value. Each object created is assigned a unique object key, stored as an integer, when it is created. These keys start at 1 and increment every time a new object of any type gets created. The object identity field is set in the System.Object constructor and cannot be modified later. Object.GetHashCode() returns this value as the hash code for a given object.

I tried to look at the documentation of Object.GetHashCode() and didn't find any information about this.

I wrote the simple piece of code to print the hash code of newly generated objects:

using System; namespace TestGetHashCode { class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { object o = new object(); Console.WriteLine(o.GetHashCode()); } } } } 

The first few numbers that were printed were:

37121646, 45592480, 57352375, 2637164, 41014879, 3888474, 25209742, 26966483, 31884011

Which didn't seem to fit that

These keys start at 1 and increment every time a new object of any type gets created...bject.GetHashCode() returns this value

Then, in order to find this "internal field in the System.Object " I tried using ReSharper decompiled sources but the code I found was

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] [__DynamicallyInvokable] public virtual int GetHashCode() { return RuntimeHelpers.GetHashCode(this); } 

and again using decompiled sources I found that RuntimeHelpers.GetHashCode was implemented as

[SecuritySafeCritical] [__DynamicallyInvokable] [MethodImpl(MethodImplOptions.InternalCall)] public static int GetHashCode(object o); 

following the MethodImpl attribute it seems that I can't view the implementation and this is a dead end for me.

Can someone please explain the comment by the author (the first quote) ?

What is the internal field within the Object class and how it is used for the implementation of the Object.GetHashCode() ?

Code snippet block removed
Source Link
Sriram Sakthivel
  • 73.8k
  • 7
  • 118
  • 200

using System; namespace TestGetHashCode { class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { object o = new object(); Console.WriteLine(o.GetHashCode()); } } } }

using System; namespace TestGetHashCode { class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { object o = new object(); Console.WriteLine(o.GetHashCode()); } } } } 

using System; namespace TestGetHashCode { class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { object o = new object(); Console.WriteLine(o.GetHashCode()); } } } }

using System; namespace TestGetHashCode { class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { object o = new object(); Console.WriteLine(o.GetHashCode()); } } } } 
Source Link
Belgi
  • 15.2k
  • 24
  • 61
  • 69
Loading