This content discusses exception handling in Microsoft .NET, focusing on application layer and reusable assemblies. It offers insights on preventing exceptions and notifying calling code, and references a related book. The principles are noted to be applicable to other programming languages.
Category: .NET Coding Standards
Serializing Objects: Efficient Serialization and Deserialization of Collections with JsonSerializer
The article demonstrates the ease of serializing and deserializing collections using JsonSerializer.
Microsoft .NET Code Analysis: Eliminating Dictionary Double Lookups
Using ContainsKey() followed by an indexer on dictionaries results in a double lookup, decreasing performance. This is common anti-pattern. This article will show you how to fix this and increase performance up to 2x.
Optimizing Collection Examination: A Comparative Analysis of Predicate Methods in C#
The article explores four methods for examining items in a collection using predicates, with a focus on performance. A Twitter poll revealed that over 50% of developers favored the LINQ Any() method, prompting the author to verify its efficiency in comparison to other options like Count() and Exists().
Code It Any Way You Want: Initializing Reference Type Static Fields Inline for Enhanced Code Readability
The article advocates for initializing reference type static fields inline to enhance code readability and prevent unexpected behavior caused by uninitialized static fields. Microsoft recommends initializing these fields at the point of declaration to improve clarity. The article provides examples demonstrating the issue and the recommended solution, emphasizing the use of static constructors for initialization.
Code It Any Way You Want: Expression-Bodied Methods vs. Traditional Methods
The article discusses the use of expression-bodied methods in .NET as an alternative to traditional methods for creating simple functions. It presents a comparison between the two methods in terms of syntax and performance. Despite the syntactical differences, benchmark results show that both methods demonstrate similar performance characteristics.
General Performance: Retrieving the Nullable Value from a Reference Type
The article discusses two methods for returning a reference type or null, highlighting their performance. It compares a ternary conditional expression to a null-coalescing operator, noting the latter is faster. Additionally, it recommends configuring EditorConfig to prioritize the null-coalescing operator for cleaner code and better performance.
Collection Performance: Exercise Caution When Using Take() with a Basic Count
The article advises caution when using the LINQ method Take() with a basic count, suggesting that a range might be recommended by code analyzers. However, benchmark results indicate that using Take() with a simple count is 1.63 times faster than employing a range in such cases.
Code It Any Way You Want: Performance Difference Between Static and Non-Static Methods
Most code analysis tools advocate for static methods due to perceived performance benefits. However, benchmarks indicate that non-static methods often outperform static ones despite avoiding instance dispatch. It's advisable to choose function types based on team coding standards. EditorConfig settings can help manage these preferences effectively.
Collection Performance: Looping Over a Collection Using Span and ReadOnlySpan
The article explores the performance benefits of utilizing Span and ReadOnlySpan for collection processing, highlighting advantages such as improved performance, reduced memory pressure, enhanced safety, and compatibility with modern API design. Span allows read-write access and efficient iteration with lightweight storage, while ReadOnlySpan provides read-only access to prevent accidental modifications, offering similar benefits for collection processing.

You must be logged in to post a comment.