more than just writing fast code

About
Company
VinteccLocation
BelgiumCompetences
Coding
C#
code performance in .NET
how small adjustments can lead to big performance gains
In an industrial context, speed and reliability are often key. Think of real-time data processing, quick decisions at the edge, or the smooth handling of thousands of transactions per second. A small inefficiency in the code can then have major consequences. It is therefore essential to program not only correctly, but also efficiently.
Exceptions: useful, but not always innocent
One of the most striking insights from the session was the effect of exception handling on performance. Exceptions are meant to handle exceptional situations - for example, when a network connection fails. But when used as normal flow control (e.g. when checking a balance), they can cost quite a bit of performance.
Although .NET 9 already brought significant improvements in this, throwing exceptions still has a huge performance impact.

Case study: payment processing
- Throw an exception:
throw new InsufficientBalanceException();
- Return a
Result
-object with a clearSuccess
orFailure
.

When should you optimise?
Optimisation only makes sense if there is a measurable problem. That is why it is important to measure before you make any changes. Tools such as Visual Studio Profiler or even a simple Stopwatch
can provide a lot of insight. Focus on code that is executed frequently or that runs within loops

Specific tips for more efficient programming
- Do not use exceptions for normal flow control.
- Choose
Span<T>
where possible – ideal for high-performance data structures. - Provide capacity when creating
List
ofDictionary
– avoid repeated allocations. - Work with
Result
-types of pattern matching instead oftry-catch
. - Measure what you change – small changes can have a big impact.
- Maintain readability – performance optimisation should not be an excuse for incomprehensible code.
