Writing high-performance C# code is a skill that can benefit any .NET developer. High-performance code can run faster, consume less memory, and scale better. Here are some tips and resources to get you started:
- Choose your data types carefully. Different data types have different performance characteristics, such as size, allocation, boxing, and casting. For example, using
int
instead oflong
when possible can save memory and improve speed. Usingstruct
instead ofclass
can avoid heap allocations and reduce garbage collection pressure1. - Use
for
loops instead offoreach
loops.foreach
loops can cause hidden allocations and boxing when iterating over collections that are not generic or implementIEnumerable<T>
for
loops can avoid these overheads and give you more control over the iteration logic1. - Use
StringBuilder
for string concatenation. Strings are immutable in C#, which means every time you modify a string, a new string object is created and the old one is discarded. This can cause a lot of memory allocations and garbage collectionStringBuilder
is a mutable class that can append strings without creating new objects1. - Use
Span<T>
andMemory<T>
for efficient data processing. These are new types introduced in C# 7.2 and .NET Core 2.1 that allow you to access and manipulate data without copying or allocating memory. They can be used to process arrays, strings, streams, buffers, and other data sources with minimal overhead23. - Use the new high-performance JSON APIs. In .NET Core 3.0, Microsoft introduced a new set of APIs for working with JSON data, such as
System.Text.Json
,JsonDocument
, andUtf8JsonWriter
. These APIs are designed to be fast, lightweight, and low-allocation, compared to the olderNewtonsoft.Json
library24. - Measure your code performance using benchmarks and profilers. The best way to know if your code is performant is to measure it using tools that can provide accurate and reliable results. Benchmark.NET is a popular library for writing and running micro-benchmarks in C#. JetBrains dotMemory is a powerful profiler that can help you analyze the memory usage and allocations of your .NET applications23.
These are just some of the tips and resources that can help you write high-performance C# code. There are many more topics and techniques that you can explore, such as parallelism, concurrency, async/await, I/O pipelines, array pools, etc. The key is to always use a data-driven approach, focus on the hot paths of your code, and apply the best practices that suit your scenario.
I hope this blog post was helpful to you. If you want to learn more about writing high-performance C# code, I recommend checking out the following links:
1: https://www.c-sharpcorner.com/UploadFile/dacca2/5-tips-to-improve-performance-of-C-Sharp-code/
3: https://www.stevejgordon.co.uk/writing-high-performance-csharp-and-dotnet-code
4: https://github.com/PacktPublishing/High-Performance-Programming-in-C-Sharp-and-.NET
5: https://stackoverflow.com/questions/183033/improve-speed-performances-in-c-sharp