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.
Category: C#
General Performance Tip: Generating Random Numbers
.NET's Random type has been a popular choice for generating random numbers. However, using RandomNumberGenerator for this purpose can result in a six-fold performance improvement, as shown in benchmark tests. Both methods allocate the same amount of memory.
Speed up Empty String Validation with Spargine
The article introduces two methods, IsEmpty() and IsNotEmpty(), for validating empty strings in Spargine, comparing them to the IsNullOrEmpty() method in .NET and emphasizing their straightforward usage and ability to handle null checks. Benchmark results reveal that IsEmpty() surpasses IsNullOrEmpty() in performance, exhibiting a 2.92 times faster execution speed.
Code It Any Way You Want: Performance of Out Variable Declaration
Starting with .NET 7, it's advised to use inline variable declarations for out parameters, enhancing performance and readability. This approach simplifies refactoring, limits variable scope, and can enable better compiler optimizations. Benchmarks indicate a 1.03x performance improvement, making code more maintainable and easier to understand.
Collection Performance: Using ForEachAsync() with List<>
The post demonstrates the usage of Parallel.ForEachAsync() with a List of reference types.
General Performance Tip: Enhanced Logging Approach
The article discusses advancements in logging techniques in .NET, particularly with the new source generator in .NET 6 that enhances performance using LoggerMessage. This new method improves logging efficiency by 11.25 times compared to previous approaches. It emphasizes the importance of extensive logging for issue resolution in code.
Optimizing String Concatenation in C# with Spargine FastStringBuilder
Explore enhanced string concatenation in C# programming with the FastStringBuilder from DotNetTips.Spargine.Core, utilizing the ConcatStrings() method for optimized performance. Benchmark results reveal a 1.107 times performance improvement and reduced memory allocations, making it a valuable tool for efficient string manipulation.
Collection Performance: Comparing Byte Array’s with SequenceEqual()
LINQ's SequenceEqual() method compares two Byte arrays, returning a Boolean indicating equal length and matching elements. Access more on collection performance by subscribing.
Speed up LINQ Any() with Spargine FastAny()
The article introduces FastAny(), a method in the DotNetTips.Spargine.Extensions NuGet package designed to accelerate LINQ's Any() call, showcasing a 1.08 times performance improvement over the standard method. Notably, FastAny() not only enhances speed but also includes a built-in null check for the collection.
General Performance Tip: Handling Exceptions
In my presentation at the Silicon Valley Code Camp, a question about the performance of using the When() clause for catching exceptions prompted me to conduct performance testing. While traditional exception handling is crucial, my testing revealed that the When() clause is more performant when capturing multiple similar exceptions.

You must be logged in to post a comment.