Csharp

Exploring the .NET CoreFX
Programming csharp
Published: 2015-03-04
Exploring the .NET CoreFX

When the .NET core framework was first released as open source in 2015, I spent a few weeks reading the source code and looking for tricks and techniques that I thought were interesting or novel. This blog post shares some of the things I found along the way.

Exploring the .NET CoreFX Part 16: Platform-Specific Builds Using Compile-Time Polymorphism
Exploring the .NET CoreFX .net core csharp
Published: 2015-03-01
Exploring the .NET CoreFX Part 16: Platform-Specific Builds Using Compile-Time Polymorphism

This is part 16/17 of my Exploring the .NET CoreFX series.

While .NET has historically been limited to Windows machines, Mono notwithstanding, the introduction of the cross-platform .NET Core runtime has introduced the possibility of running .NET Core applications on Unix machines. With this possibility, developers may have the need of writing platform-specific code.

One way to write platform-specific code is:

  1. Define a conceptual base class which will have an identical name and methods across all platforms. This does not need to be a C# interface, as we will be using compile-time rather than run-time polymorphism.
  2. Provide an implementation of this class for each target platform.
  3. Use build-time conditions to include the platform-specific class based on target compilation platform.

An example from the .NET Core is the System.Console.ConsolePal class from the System.Console library. The library includes two implementations of this class:

Read more...
Exploring the .NET CoreFX Part 15: Using Non-Generic Factory Classes to Enable Type Inference
Exploring the .NET CoreFX .net core csharp system.collections.immutable
Published: 2015-02-18
Exploring the .NET CoreFX Part 15: Using Non-Generic Factory Classes to Enable Type Inference

This is part 15/17 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:

1
2
3
4
5
6
7
8
9
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:

Read more...
Exploring the .NET CoreFX Part 13: ImmutableList is an AVL Tree
Exploring the .NET CoreFX .net core csharp system.collections.immutable
Published: 2015-01-13
Exploring the .NET CoreFX Part 13: ImmutableList is an AVL Tree

This is part 13/17 of my Exploring the .NET CoreFX series.

Most implementations of IList, including System.Collections.Generic.List, are dynamic arrays. System.Collections.Immutable.ImmutableList is different – it is an AVL tree. This results in significantly different performance characteristics:

List ImmutableList
Indexing O(1) O(log n)
Append O(1) average, O(n) worst-case O(log n)
Insert at arbitrary index O(n) O(log n)
Remove O(n) O(log n)
Memory layout Contiguous for value types Non-contiguous

The data structure behind ImmutableList was likely chosen so that modifications to the list are non-destructive and require minimal data copying.

Read more...