C# is null vs == null

2023-12-24

When C# 7.0 was officially released in March 2017 it introduced several new features to make the life of developers easier, like tuples and deconstruction, local functions, expression body definitions and, the focus of this article, pattern matching. One of the advantages of pattern matching is a more concise syntax for testing expressions and taking actions when there’s a match, increasing the readability and correctness of your code. Checking for nulls is one of the most common usages. Before C# 7.0, if a developer wanted to check if a given value was null, usually the equality comparison would be used. With pattern matching, the extension method would be using the is operator and the null constant to check if the person variable is null. The change is so small that you are probably questioning if changing the way you code is really worthy for such a small readability improvement, to have the same results? And you are right in thinking that way since that’s probably true for 99% of use cases (if not 99.999%) but what if you really want to be sure it works every time? You see, reference types can overload the == operator and, if the developer decides to always return false when comparing to null, the first example would throw a NullReferenceException. Let’s test that by overloading the == operator to always return false, whatever the parameters received.

Link [ https://levelup.gitconnected.com/c-is-null-vs-null-5b3a80ecb620 ]

Previous Article

C# is null vs == null

2023-12-24

When C# 7.0 was officially released in March 2017 it introduced several new features to make the life of developers easier, like tuples and deconstruction, local functions, expression body definitions and, the focus of this article, pattern matching. One of the advantages of pattern matching is a more concise syntax for testing expressions and taking actions when there’s a match, increasing the readability and correctness of your code. Checking for nulls is one of the most common usages. Before C# 7.0, if a developer wanted to check if a given value was null, usually the equality comparison would be used. With pattern matching, the extension method would be using the is operator and the null constant to check if the person variable is null. The change is so small that you are probably questioning if changing the way you code is really worthy for such a small readability improvement, to have the same results? And you are right in thinking that way since that’s probably true for 99% of use cases (if not 99.999%) but what if you really want to be sure it works every time? You see, reference types can overload the == operator and, if the developer decides to always return false when comparing to null, the first example would throw a NullReferenceException. Let’s test that by overloading the == operator to always return false, whatever the parameters received.

Link [ https://levelup.gitconnected.com/c-is-null-vs-null-5b3a80ecb620 ]

Copyright © 2024 All rights reserved

Rss

Atom