Coding Standards: Mastering Tuples in .NET

In .NET, tuples are immutable data structures that group multiple values of different types, simplifying code by avoiding custom classes. They are useful for returning or passing multiple values. Named tuples enhance readability but should not be overused. Accessing tuple elements can be done by position or via deconstruction.

String Performance: Comparing Strings with Globalization

In the String Performance chapter of the code performance book, the author demonstrates the usage of string.Compare() with StringComparison. Benchmark results are provided, along with a recommendation to use cultures or ordinals based on the strings being compared. The chapter also touches on setting up EditorConfig for dotnet_diagnostic.CA1862.

String Performance: String Compression

The content discusses the significance of string compression in programming, outlining four available data formats and demonstrating how to compress and decompress strings using Brotli. It also mentions benchmark results and a tool called Spargine for streamlining the process. For more details, visit the provided link and subscribe for access to exclusive content.

Collection Performance: Iterating Through Reference Value, and Record Types

The author delves into the performance disparities of using reference types, value types, and record types in collections.

General Performance Tip: Constant vs Variable

It is recommended to use constants for static numerical or string values in code. This helps maintain code clarity and adhere to best practices. Benchmark tests show variables are slightly more performant than constants, but it is still advisable to use constants where appropriate to reflect the intent of the variable.

Collection Expressions in .NET 8

Beginning with .NET 8, you have the option to employ the novel collection expression to generate frequently used collection values. A collection expression comprises a concise syntax, which, upon evaluation, can be assigned to a wide array of collection types. Key Features of Collection Expressions: Concise Syntax: Easily create collections using square brackets [] and comma-separated elements, such as … Continue reading Collection Expressions in .NET 8

General Performance Tip: Null Coalescing Assignment

In C# 8, a new method for null coalescing assignments was introduced, simplifying the code. Performance benchmark results show a slight advantage in using the traditional approach for null checking compared to the new method. Previously, the performance difference between the two methods was negligible.

General Performance Tip: Performance Impact of Sealing Attributes

When customizing attributes, Microsoft and I both advise sealing them for clarity and performance. However, benchmark results show sealing attributes to be marginally less performant, with both outcomes indicating a memory allocation of 24 bytes. Despite this, I maintain that sealing non-inheritable classes is essential for robust class design.

Collection Performance: Memory Efficiency with AsMemory() in Byte Array Conversion

The article discusses the efficiency benefits of using AsMemory() for byte array conversion, emphasizing reduced memory usage, future-proofing code, and performance optimization. Benchmark results highlight a significant performance advantage, with a 54 times improvement compared to other methods, reinforcing the importance of AsMemory() for optimal performance in memory-sensitive applications.

Collection Performance: Converting Byte Array to Memory<> and ReadOnlyMemory<>

When working with byte arrays, there are two methods to convert to Memory. Using AsMemory() offers performance optimization, efficient memory management, improved code clarity, and future-proofing. Benchmark results show a 57x performance improvement, with no memory allocation. This method is crucial for memory-sensitive applications and large datasets. Utilize AsMemory() for optimal performance and efficient array handling.