Immutability In C#

I’ve struggled with how to allow immutable objects in object-oriented languages like C#, Java, and C++, either at the application level or with new language features. Here are a few thoughts. It’s not so clear that immutable instances should be created in the constructor. For example, if an application has a complicated List<T> instance, how […]

The System.Random Class Should Have an Abstract Base Class or Interface

The base class library has a significant flaw with respect to System.Random: there is no abstract base class or interface for random number generators with the same interface as System.Random. (The System.Security.Cryptography namespace does have the RandomNumberGenerator base class, but that type’s interface differs from System.Random‘s.) The situation is as though someone designed a type […]

C# 64-Bit Array.Clear

In version 3.5 of the NET framework, and possibly in earlier releases, there are variants of Array.Copy that accept 64-bit values for index and length. Unfortunately, Array.Clear has no such variant. This seems to leave only bad options for clearing an array using 64-bit indexes and lengths. One option is to cast the arguments to […]

Performance Tip: Prefer (((Object) x)== null) to Object.ReferenceEquals(x,null)

What’s the best way in C# to test for a null reference? It turns out that there are a few different ways of doing this correctly, and that they have different speed and code size characteristics. Consider overloading the == operator for some class Thing. A natural first attempt is as follows. public static bool […]

Shorter Construction Syntax

Did you ever notice the redundancy that’s forced by C#’s syntax for invoking and defining constructors? using System.Collections.Generic; class Thing { Thing(int x, int y) { … } public static void Main() { Thing a = new Thing(3,2); Dictionary<string,double> b = new Dictionary<string,double>(); } } It might be nice if C# added a less redundant […]

The “Structs Should Always be Immutable” Guideline

Sometimes one comes across the following guideline for C# structs: All structs should be immutable. The results of searching the web for justification for this are not very satisfying. One argument sometimes advanced is that an immutable struct is usually simpler to understand than a mutable struct. This argument is true, but it also applies […]

A non-Obvious Benefit of Replacing Boolean Arguments with Enumerations

When a method takes a boolean argument, it is sometimes better to restructure the method so that it takes an enumeration argument. In this article we show that the real benefit of this is not obvious and is often misunderstood. Consider the following method and one of its clients: void search(bool useCaseSensitiveSearch) { … } […]

C# 2.0: 3f.Equals(3) != 3.Equals(3f)

In C#, it is good practice to make the Equals method commute.  But even the Base Class Library fails to do this sometimes.  It’s possible to have a.Equals(b) != b.Equals(a) even with primitive types, e.g, when a is a Single (a float) and b is an Int32. http://blogs.msdn.com/jmstall/archive/2005/03/23/401038.aspx