Sorting in .NET

Sorting is a typical task each programmer should be aware of. That’s why this article is dedicated to the implementation of sorting in .NET. I will describe how array sorting works in .NET, its aspects, and make a small comparison with sorting in Java.

Let’s begin with the fact that the first versions of .NET use the quicksort algorithm by default. So, let’s consider pros and cons of the quicksort.

Pros

  1. One of the most high-performance algorithms(on a practical level) of general-purpose internal sorting.
  2. Easy implementation.
  3. Requires just O(logn) of additional memory for its operation.
  4. Can be easily combined with cashing and internal memory mechanisms.

(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…)

StringBuilder: the Past and the Future

In the previous article, I elaborated on peculiarities of string concatenation. In this article, I would like to consider the StringBuilder class in detail.

As we all know, strings in .Net are immutable (without use of unsafe). Therefore, it is not a good idea to perform concatenation frequently. It means that the following code has quite serious problems with memory load:

So, what is wrong with this code?
(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…)

Foreach or For – That is the Question

The discussion about the preference difference between FOREACH and FOR is not new. We all know that FOREACH is slower, but not all know why.

When I started learning .NET, one person told me that FOREACH  is two times slower than FOR. He said this without any grounds. I took it for granted.

Eventually, I decided to explore the performance of both loops, and write this article to discuss nuances. (more…)

Long Arithmetic from Microsoft

It is known, a computer can operate numbers with a limited number of bits. As a rule, we are accustomed to work with the 32-bit and 64-bit integers. On the .Net platform, the Int32 (int) and Int64 (long) types correspond to these integers.

But what to do if we need to represent, for instance, number 29! = 8841761993739701954543616000000? Such number won’t fit both 32-bit and 64-bit data types. Long arithmetic is designed specifically for working with such big numbers.

In computing technology, long arithmetic implies operations (addition, multiplication, subtraction, division, raising to a power etc.) with numbers, the bitness of which exceeds the length word of the given computer. These operations are implemented not by hardware but by software with the help of basic hardware for working with small-order numbers.

(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…)

Under the Hood of Stopwatch

Introduction

As all developers, I often need to measure execution time of my own (and not only my own) code. When I was a beginning programmer, I used the DateTime structure for his purpose. Time have passed and I learned about the Stopwatch class and began using it extensively. I think most of you had similar experience. It’s not like I didn’t wonder about how Stopwatch works, at that time, it was simply enough for me to know that Stopwatch measures the elapsed time more precise than DateTime. Now the time has come to explain to myself as well as to the readers how the Stopwatch class actually works and clarify its pros and cons in comparison with DateTIme.
(more…)

CHECK Constraints in SQL Server

In this article, we will talk about CHECK constraints. We will see how to add CHECK constraints to SQL Server table columns and discuss the pitfalls you may encounter when using this type of SQL Server constraints.

CHECK constraint basics

CHECK constraints are simply conditional statements (predicates that return TRUE or FALSE) that refer to table columns for maintaining data integrity. When one inserts data into a column or several columns in a single row, CHECK constraints come into action. They evaluate the data to be inserted. In case the data does not meet the condition specified in the CHECK constraint, the insertion fails.

(more…)