Understanding the differences between reference types (classes) and value types (structs) is crucial for optimizing .NET application performance. This content offers benchmark-driven insights on when to use each type, emphasizing the impact of choice on execution efficiency. Mastering these concepts ensures high-performance code in your applications.
Category: Coding Standards
Collection Performance: Is LINQ Always the Most Performant Choice?
The article explores the performance implications of using LINQ for collection queries, finding that a conventional foreach() loop outperforms LINQ by 1.75 times in identifying items matching a given query. The conclusion suggests benchmarking to determine the optimal approach based on the nature of the query and elements being sought.
Code It Any Way You Want: Comparison of Passing Parameters in Methods
This article explores different methods of passing parameters into methods, including conventional, in operator, and ref readonly approaches, comparing their performance. Despite differences in syntax, benchmark results demonstrate similar performance among these methods.
Code It Any Way You Want: Optimal Parameter Passing – Array vs. Params Keyword
The article explores the performance differences between passing parameters as arrays or using the params keyword in C#. Despite similarities in speed, the author recommends using the params keyword for its ease of use during function calls.
Collection Performance: Creating a List<> Using The Task.Parallel Library
The post discusses alternative approaches to adding items to a collection using For() or ForEach() from the Task Parallel Library.
General Performance Tip: Can Pattern Matching Improve Performance?
The article discusses the potential performance improvements gained by leveraging pattern matching in .NET. It contrasts a traditional method for rounding numbers with a more refined version employing pattern matching.
Code It Any Way You Want: Checking Strings for Null
The article discusses best practices for checking strings for null in coding. It outlines three common methods: using == null, is null, or string.IsNullOrEmpty().
General Performance Tips
To enhance the performance of .NET applications, developers should focus on mastering key techniques rather than relying on quick fixes. This resource offers a comprehensive collection of practical, evidence-based tips covering a range of topics, from memory management to coding patterns, aimed at creating efficient, robust applications ready for production.
Reference Type & Structure Performance: Hashing Classes, Records, and Structures
A SHA256 hash can be generated for a class, record, or structure using a specific code. Performance analysis shows that hashing a class is 1.032 times faster than a structure and 1.048 times more efficient than a record. The memory allocations for record, reference, and value types are also provided in bytes.
General Performance Tip: Cloning Various Types
The post discusses the process of cloning different types of objects, with Microsoft recommending the use of JSON serialization for this purpose. The author explores the performance differences in cloning reference types, value types, and record types. Benchmark results using JsonSerializer reveal that cloning reference types is 1.015 times faster than cloning record types and 1.24 times more performant than cloning value types. The findings are shared for reference.

You must be logged in to post a comment.