System.collections.immutable

This is part 15 of my Exploring the .NET CoreFX Series.
While C# supports type inference for generic methods, it does not support type inference for constructors. In other words, while this code works:
public class FooFactory { public static Foo<T> Create<T>(T value) { return new Foo<T>(value); } } var myObj = FooFactory.Create(212); This code does not:
public class Foo<T> { private readonly T field; public Foo(T value) { field = value; } } var obj = new Foo(212); // DOES NOT WORK For more background on why this is, see this StackOverflow post.

This is part 14 of my Exploring the .NET CoreFX Series.
Back in 2013, Immo Landwerth and Andrew Arnott recorded a Going Deep video called Inside Immutable Collections which describes how and why System.Collections.Immutable is built the way it is. It’s great background material to understand System.Collections.Immutable.

This is part 12 of my Exploring the .NET CoreFX Series.
In C++, the inline keyword allows a developer to provide a hint to the compiler that a particular method should be inlined. C# has the identical ability but uses an attribute instead:
internal class SecurePooledObject<T> { ....[MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool IsOwned<TCaller>(ref TCaller caller) where TCaller : struct, ISecurePooledObjectUser { return caller.PoolUserId == _owner; } } In System.Collections.Immutable, this attribute is used highly selectively – only once, in fact.

This is part 11 of my Exploring the .NET CoreFX Series.
In 2008, Microsoft Research published Code Contracts, which provide a language-agnostic way to express coding assumptions in .NET programs. The assumptions take the form of pre-conditions, post-conditions, and object invariants.
Here is a simple example of code which uses Code Contracts:
using System.Diagnostics.Contracts; public class StringUtils { internal static string Append(string s1, string s2) { Contract.Requires(s1 != null); Contract.

This is part 10 of my Exploring the .NET CoreFX Series.
The .NET Core’s System.Collections.Immutable.ImmutableArray provides two enumerators. The first has been highly tuned for speed, and the second is a fallback for compatibility when it is required.
The high-performance enumerator uses the following performance optimizations:
The enumerator is a struct, rather than a class, so that it is stack-allocated rather than heap-allocated. The enumerator does not implement IEnumerator or IEnumerator, as this would require it to implement IDisposable.

This is part 9 of my Exploring the .NET CoreFX Series.
Using the builder pattern to allow for easier construction of immutable objects is well-known.
The .NET Core’s immutable collections assembly, System.Collections.Immutable, also uses the builder pattern, but for a slightly different reason: to improve the performance of making many changes to the collection. This is possible because, unlike the immutable collection itself, the builder pattern does not need to maintain the immutable collection’s invariants after each modification.

This is part 8 of my Exploring the .NET CoreFX Series.
The .NET Core’s System.Collections.Immutable.ImmutableArray class implements an immutable wrapper around a normal C# managed array. This looks something like:
public struct ImmutableArray<T> { internal T[] array; ... } ImmutableArray.array is lazy-initialized.
Within the ImmutableArray class, there are a number of methods which have the precondition that ImmutableArray.array must be initialized. These preconditions must be checked before the method begins processing to make sure we handle invalid states correctly.

This is part 7 of my Exploring the .NET CoreFX Series.
In the previous post, I referenced EqualityComparer.Default. If T does not implement IEquatable, EqualityComparer.Default will use the framework-defined Object.Equals(), which implements reference equality.
However, many times you want to compare two types for structural equality (i.e. identical content) rather than reference equality (i.e. two references point to the same instance of the class). The interface IStructuralEquatable was defined to allow a class to explicitly implement structural, rather than reference equality.