Performance implications of default struct equality in C#

If you’re familiar with C#, then you most likely heard that you should always override Equals and GetHashCode for custom structs for performance reasons. To better understand the importance and the rationale behind this advice we’re going to look at the default behavior to see why and where the performance hit comes from. Then we’ll…

2

Dissecting new generic constraints in C# 7.3

During the last Build conference, Microsoft has announced the next version of Visual Studio with C# 7.3 support. This is yet another minor language update with some quite interesting features. The main change was related to generics, starting from C# 7.3 there 3 more constraints: unmanaged, System.Enum and System.Delegate. The unmanaged constraint The unmanaged constraint…

9

Avoiding struct and readonly reference performance pitfalls with ErrorProne.NET

As you may know from my previous posts “The ‘in’-modifier and the readonly structs in C#” and “Performance traps of ref locals and ref returns in C#”, structs are trickier then you might think. Mutability aside, the behavior of readonly and non-readonly structs in “readonly” contexts is very different. Structs are meant for high-performance scenarios…

3

Performance traps of ref locals and ref returns in C#

The C# language from the very first version supported passing arguments by value or by reference. But before C# 7 the C# compiler supported only one way of returning a value from a method (or a property) – returning by value. This has been changed in C# 7 with two new features: ref returns and…

0

Nullable types arithmetic and null-coalescing operator precedence

Here is a simple question for you: which version of a GetHashCode() is correct and what performance impact does the incorrect version have? public struct Struct1{    public int N { get; }    public string S { get; }    public Struct1(int n, string s = null) { N = n; S = s; }     public override int GetHashCode() =>         N ^         S?.GetHashCode()…

0

The ‘in’-modifier and the readonly structs in C#

C# 7.2 got two very important features for high-performance scenarios — the readonly structs and the in parameters. But to understand why this additions are so important and how they’re related to each other we should look back in history. As you probably know, the .NET ecosystem has two family of types — the value…

4

One user scenario to rule them all

The async series Dissecting the async methods in C#. Extending the async methods in C#. The performance characteristics of the async methods in C#. One user scenario to rule them all. Almost every non-trivial behavior of the async methods in C# can be explained based on one user scenario: migration of the existing synchronous code…

4

The performance characteristics of async methods in C#

The async series Dissecting the async methods in C#. Extending the async methods in C#. The performance characteristics of the async methods in C#. One user scenario to rule them all. In the last two blog posts we’ve covered the internals of async methods in C# and then we looked at the extensibility points the…

3

Extending the async methods in C#

The async series Dissecting the async methods in C#. Extending the async methods in C#. The performance characteristics of the async methods in C#. One user scenario to rule them all. In the previous blog post we discussed how the C# compiler transforms asynchronous methods. In this post, we’ll focus on extensibility points the C#…

3

Dissecting the async methods in C#

The async series Dissecting the async methods in C#. Extending the async methods in C#. The performance characteristics of the async methods in C#. One user scenario to rule them all. The C# language is great for developer’s productivity and I’m glad for the recent push towards making it more suitable for high-performance applications. Here…

12