CODE Presents: Improving String Handling Performance in .NET 7

Presenter: Joydip Kanjilal

This event took place on Wednesday, November 16, 2022.

Download Slides


Watch the Video Recording

(Webinar is updated for .NET 7.) A string is an immutable type, which means that once you have created a string object, you cannot change it in any way. When you change or modify a string object, a new instance is created. There are several ways to overcome this performance issue. This session talks about how strings are represented in the CLR memory, the performance challenges you would often encounter when working with strings and how to fix them in C#.

The following points are covered:

An Introduction to the CLR

  • The Garbage Collector
  • Generations

Performance Challenges of Strings in .NET

  • How are strings represented in the memory?
  • What is a String?
  • String layout in memory
  • When are strings allocated?

Improving string handling performance

  • String Interning
  • Use Append.Join for Joining Strings
  • Using StringBuilders

Improving String Concatenation Performance

  • Reducing allocations
    1. Set the Capacity of StringBuilder Instances
    2. Use a Pool of StringBuilder Instances
    3. Using StringBuilderCache
    4. Using String.Create
    5. Using Span and ReadOnlySpan
    6. Using Memory and ReadOnlyMemory

Benchmark Performance using BenchmarkDotNet


Wednesday, November 16, 2022 1:00pm ET (UTC-5)
Duration: 60 minutes (approximately)