SoFunction
Updated on 2025-03-06

Summary of the technical processing of NULL in C# language

Preface

In C#, null is a special existence that indicates a missing value. Null can be assigned to reference types and nullable value types, but cannot be assigned to non-nullable value types. Handling null values ​​in C# is an important aspect of writing reliable and reliable code. In this article, I will discuss some of the most common techniques for handling null values ​​in C#.

1. NULL merge operator (??)

The null merge operator (??) is an abbreviation operator that returns the left value when the left object is not null and the right value when the left object is null. This operator is useful when you are judging whether the current object value is null and the assignment of a new object requires abbreviation.

string name = null;
string result = name ?? "Unknown";
(result); // Output: Unknown
In the above example,becausenameThe value ofnull,Therefore usenullThe merge operator will“Unknown”Assign the value toresult。

2. Conditional operator (?😃

The condition operator (?:) is a ternary operator, used instead of the abbreviation of if-else, returns a value if the condition is true, and another value if the condition is false. This operator is useful when you need to provide default values ​​based on conditions.

string name = null;
string result = (name != null) ? name : "Unknown";
(result); // Output: Unknown
In the above example,becausenameThe value ofnull,Therefore, using the conditional operator will“Unknown”Assign the value toresult。

3. The empty condition operator (?.)

If the object is not empty, the null condition operator (?.) is used to access the object's members, otherwise it returns empty. This operator is useful when you need to access properties or methods of objects that may be empty.

string name = null;
int length = name?.Length??0;
(length); // Output: 0

In the above example, since the value of name is empty, the value of length is also empty.

But you can also use the merge operation operator (??) for advanced writing, the code is as follows:

string name = null;
int length = name?.Length??0;
(length); // Output: 0

You can see in the above code example, length can be used to obtain the length of name without the empty value type. In your business logic, you can think that the length of null's string can also be 0 length, reducing the complex judgment of later value types.

4. Empty merge assignment operator (??=)

The empty merge assignment operator (??=) is used to assign the right object to the left when the left object is empty. It means that if the variable object is empty, you can assign a default object value to it, which can simplify the code and make it more concise in understanding.

string name = null;
name ??= "Unknown";
(name); // Output: Unknown

In the example above, since the value of name is empty, the value of "Unknown" is assigned to the name using the empty merge assignment operator.

5. Tolerance operator (!.)

The allowable null operator (!.) is used to tell the compiler that the value is not empty, even if the compiler cannot determine that the value is not empty. This operator is useful when you know that a value will not be empty, but the compiler cannot determine that it will not be empty.

string name = null!;
int length = ;
(length); // Output: 

In the above example, the tolerance operator is used to tell the compiler that the value of the name cannot be null. However, since the value of name is actually empty, the system throws a NullReferenceException at runtime.

6. Use the is operator

In the C# programming language, the "is" operator is a keyword that checks whether a variable is compatible with a specific type. With the introduction of constant mode in C# 7.0, this operator gains additional functionality. Specifically, the value can now be compared to a constant (such as null) using the "is" operator.

if (name is null)
{
   // code to execute
}
(name);

Extensions using the "is" operator enable developers to write cleaner and more readable code when performing type checking and comparisons in C# applications.

Alternatively, you can check with operator:==null

object obj = null;
if (obj == null)
{
    // do something if obj is null
}
else
{
    // do something else if obj is not null
}

Both methods are OK, but checking with operators may be more readable and expressive, especially when used in conjunction with other type checks is null .

7. ArgumentNullException parameter empty exception

Finally, the ArgumentNullException class is used to handle null parameters passed to a method or constructor. This class is useful when you need to make sure that the parameters passed to the method or constructor are not empty.

public void DoSomething(string name)
{
    if (name == null)
    {
        throw new ArgumentNullException(nameof(name));
    }
    ($"Hello, {name}!");
}
// Usage
DoSomething(null); // Output: : Value cannot be null. (Parameter 'name')

In the example above, the ArgumentNullException class is used to throw an exception when the value of name is empty.

in conclusion

In software development, handling null values ​​is very important. We discuss 7 techniques for handling null values ​​in C#. Additionally, handling null values ​​in C# is also important to avoid null reference exceptions and other unexpected behaviors.

This is the end of this article about the technical processing of NULL in C# language. For more related C# NULL technical processing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!