Covariant and Contravariant Casting is 3x Slower than Dynamic

Previously, we saw that covariant and contravariant casting is slow: 100x slower than normal casting. It turns out that covariant and contravariant casting is even slower than casting to dynamic and using dynamic dispatch: 3x slower than dynamic. This is significant as IEnumerable<T>

Read More

Casting to IEnumerable<T> is Two Orders of Magnitude Slower

Casting to generic interfaces that use covariance or contravariance is two orders of magnitude slower than normal casts in C#. This means casting to IEnumerable<T> is two orders of magnitude slower too. This result was quite unexpected and very surprising.

In this post, I investigate the cost of casting to implementations, interfaces, generic interfaces, covariant …

Read More

C# 7: Micro-Benchmarking the Three Ways to Cast Safely

As we saw in my previous post, there are three ways to cast safely in C# 7. In this post, I micro-benchmark the three methods of safe casting and dive into the IL to understand the differences.

The three methods of safe casting (from my previous post

Read More

C# 7: Is Operator Patterns - You won't need 'as' as often

C# has long supported two operators to check the type of an object: is and as. C# 7 adds a new way to use the is operator that combines is with basic patterns to provide an alternative for as.

The new is patterns provide a nicer syntax for safe casting than both the existing is and as

Read More

C# 7: Ref Returns, Ref Locals, and how to use them

C# has always supported the ability to pass by reference using the ref keyword on method parameters. C# 7 adds the ability to return by reference and to store references in local variables.

The primary reason for using ref returns and ref locals is performance. If you have big structs, you can now reference these directly in safe code to avoid copying. Before C# 7 you had …

Read More

C# 7: Dynamic types and Reflection cannot access Tuple fields by name

Tuples are a great new feature in C# 7 and I've used them a few times already. The syntax is great, they're easy to use, and they're a whole lot better than the alternatives.

Tuples are implemented using ValueTuple, with name erasure at runtime

Read More

C# 7: Local Functions are Funcs too

Local functions in C# 7 are versatile: they can be passed as Func<> and Action<> to other methods and they can be defined using the inline expression bodied syntax.

Because local functions are compiled into static methods, you should be able to pass them to any method that requires a Func<> or Action<> and sure …

Read More

C# 7: Dissecting Local Functions to understand how they capture local variables

One of the new language features in C# 7 is local functions. This feature lets you define functions within the scope of other functions. The main benefit of local functions is encapsulation and a secondary benefit is that they bring local variables into scope.

Although local functions appear simple, their interaction with other language features quickly leads to questions. I wondered …

Read More

C# 7 Preview: Using tuples to return multiple values

Microsoft is hard at work on C# 7, the next version of C#. In this post, I'll look at tuples, one of the new features proposed for C# 7. So what are tuples, why do we need them, and how do they differ from what we have already.

Tuples are a way to temporarily group multiple values together. Currently, C# handles inputs and outputs asymmetrically. There is syntactic support for passing …

Read More