In this article, we will discuss typical errors that newbie developers may face with while designing T-SQL code. In addition, we will have a look at the best practices and some useful tips that may help you when working with SQL Server, as well as workarounds to improve performance.
In the previous article, we analyzed how to compare objects by value on a particular example with the Person class that includes:
- Overriding the GetHashCode() andObject.Equals(Object) methods;
- Implementation of the IEquatable (Of T) interface;
- Implementation of the type-specific static method Equals(Person, Person) and operators ==(Person, Person), !=(Person, Person).
Now, we will explore a type-specific implementation of how to compare objects by value including the IEquatable(Of T) generic interface and overload of “==” and “!=” operators.
Type-specific comparison of objects by value allows achieving:
- a more stable, scalable and mnemonic (readable) code through overloaded operators;
- higher performance.
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).
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:
- What is the minimum required number of class modifications to assure comparing class objects by values with the help of the standard .NET architecture?
- 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.
In the object-oriented programming world, the concept of inheritance has been criticized for a long time.
There are quite a lot of arguments:
- A derived class inherits all the parent’s data and behavior that is not always necessary. When modifying a parent class, some data and behavior that are not supposed to be in the derived class get into it;
- Virtual methods are less productive. If a language allows declaring a non-virtual method, what would you do when you need to override one in an inherited method? You can mark the method with the new keyword, however, in this case, polymorphism will not work. The consequent use of this object may cause unexpected behavior, depending on the type the object is going to be converted to.
- The vast majority of languages do not allow multiple inheritance.
- There are tasks where inheritance is helpless. Consider the case, when you need to create a container (array, list, or set) and implement the same behavior for elements with different types. In addition, you need to provide static typing. In this case, generics will come to help.
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.
- One of the most high-performance algorithms(on a practical level) of general-purpose internal sorting.
- Easy implementation.
- Requires just O(logn) of additional memory for its operation.
- Can be easily combined with cashing and internal memory mechanisms.
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…)
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:
string s = string.Empty;
for (int i = 0; i < 100; i++)
s += "T";
So, what is wrong with this code?
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).