The article discusses best practices for checking strings for null in coding. It outlines three common methods: using == null, is null, or string.IsNullOrEmpty().
Category: Defensive Programming
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. This article shows the differences.
General Performance: Cloning Reference, Value, and Record Types
Microsoft suggests cloning objects via JSON serialization and deserialization. Performance tests show that cloning record types is the fastest method, outpacing reference types by 1.087Ă— and value types by 1.26Ă—. While differences are modest, these results are significant for object cloning efficiency.
String Performance: Optimizing String Substring Extraction – Slicing vs. AsSpan()
The content presents an alternative approach to obtaining a substring using slicing with ReadOnlySpan and AsSpan() methods.
General Performance: Optimizing Method Returns
Modern best practices in method design emphasize returning empty collections instead of null values when no items are found, enhancing code safety and clarity.
Collection Performance: Comparing Key Search Methods in Dictionaries
The post discusses two methods for searching a specific key in a Dictionary. The ContainsKey() method is recommended by Microsoft for improved performance but is it more performant?
String Performance Tip: Verifying if an Object is a String
The post explains two methods to confirm if an object is a string. It provides code examples for both methods and states that the first method is 1.17 times more efficient than pattern matching.
General Performance Tip: Constant vs Read-Only Property
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.
General Performance: Optimizing Random Number Generation
The .NET Random type generates random numbers effectively, but using RandomNumberGenerator is significantly more efficient, offering a 7.08Ă— speed increase. Both methods allocate equal memory; however, RandomNumberGenerator is preferable for scenarios requiring cryptographic strength or scalability. It enhances performance while ensuring security in random number generation.
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.

You must be logged in to post a comment.