Comparing Objects by Value. Part 2. Implementation Notes of the Equals Method

In the previous article, we have reviewed a general concept of implementing a minimum set of required modifications that include overriding the Object.Equals(Object) and Object.GetHashCode() methods in order to compare class objects by value on a standard .NET framework.

Let’s consider the implementation features of the Object.Equals(Object) method so that it meets the following documentation requirement:

x.Equals(y) returns the same value as y.Equals(x).

(more…)

Comparing Objects by Value. Part 1. Beginning

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.

(more…)

Aspects of Strings in .NET

The string data type is one of the most significant data types in any programming language. You can hardly write a useful program without it. Nevertheless, many developers do not know certain aspects of this type. Therefore, let’s consider these aspects.

Representation of strings in memory

In .Net, strings are located according to the BSTR (Basic string or binary string) rule. This method of string data representation is used in COM (the word ‘basic’ originates from the Visual Basic programming language in which it was initially used). As we know, PWSZ (Pointer to Wide-character String, Zero-terminated) is used in C/C++ for representation of strings. With such location in memory, a null-terminated is located in the end of a string. This terminator allows to determine the end of the string. The string length in PWSZ is limited only by a volume of free space. (more…)

The origin of GetHashCode in .NET

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).

(more…)

Is string operator “+” so simple?

Introduction

A string data type is one of the fundamental data types, along with numeric (int, long, double) and logical (Boolean) ones. You can hardly imagine at least one useful program that does not utilize this type.

On the .NET platform, the string type is presented as an immutable String class. In addition, it is strongly integrated into the CLR environment and is also supported by the C# compiler.

This article is devoted to concatenation – an operation performed on strings as often as the addition operation on numerals. You may think: “What is there to say?”, after all, we all know about string operator “+”, but as it turned out, it has its own quirks.

(more…)