Comparing Objects by Value. Part 5: Structure Equality Issue

Comparing Objects by Value. Part 5: Structure Equality Issue
Rate this post

In my previous publication, I described the fullest and the most correct way to compare objects by value – class instances that represent reference types in the .NET framework.

Now, I am going to determine how it is possible to modify the proposed method to compare instances of the structs that represent value types.

Read More

Comparing Objects by Value. Part 4: Inheritance & Comparison Operators

Comparing Objects by Value. Part 4: Inheritance & Comparison Operators
3.7 (73.33%) 3 votes

In the previous article, we analyzed how to compare objects by value on a particular example with the Person class that includes:

Read More

Comparing Objects by Value. Part 1. Beginning

Comparing Objects by Value. Part 1. Beginning
4 (80%) 1 vote

It is a common fact that the .NET object model, as well as other software program platforms, allow comparing objects by reference and by value.

By default, two objects are equal if the corresponding object variables have the same reference. Otherwise, they are different.

However, in some cases, you may need to state that two objects belonging to the same class are equal if their content match in a certain way.

Assume we have the Person class, which contains some personal data – First Name, Last Name, and Birth date.

Consider the following points:

  1. What is the minimum required number of class modifications to assure comparing class objects by values with the help of the standard .NET architecture?
  2. What is the minimum required number of class modifications to assure comparing class objects by values (every time, if not explicitly stated that objects may be compared by a reference) with the help of the standard .NET architecture?

For each case, we will see the best way to compare objects by value to get a consistent, compact, copy-paste free, and productive code. It is not as trivial as it may seem for the first time.

Read More

The origin of GetHashCode in .NET

The origin of GetHashCode in .NET
3.7 (73.33%) 3 votes

This article is devoted to the GetHashCode method and the GetHashCode implementation in the .NET Framework. The article also discusses the different behavior of the method for reference types and value types. The topic is quite interesting and any self-respecting .NET developer needs to know it. So let’s go!

What’s stored in reference-type objects apart from their field?

Let’s begin our story with learning what is stored in reference-type objects in addition to their fields.

Each reference type object has the so-called header, which consists of two fields: a pointer to the type of the object (MethodTablePointer), as well as a synchronization index (SyncBlockIndex).

Read More