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…

2

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

Dissecting the tuples in C# 7

System.Tuple types were introduced in .NET 4.0 with two significant drawbacks: (1) tuple types are classes and (2) there was no language support for constructing/deconstructing them. To solve these issues, C# 7 introduces new language feature as well as a new family of types (*). Today, if you need to glue together two values to…

7

Dissecting the pattern matching in C# 7

C# 7 finally introduced a long-awaited feature called “pattern matching”. If you’re familiar with functional languages like F# you may be slightly disappointed with this feature in its current state, but even today it can simplify your code in a variety of different scenarios. Every new feature is fraught with danger for a developer working…

13

Dissecting the local functions in C# 7

The Local functions is a new feature in C# 7 that allows defining a function inside another function. When to use a local function? The main idea of local functions is very similar to anonymous methods: in some cases creating a named function is too expensive in terms of cognitive load on a reader. Sometimes…

22