SoFunction
Updated on 2025-03-04

Summary of the simplified assignment method of C# to NULL

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 example above, since the value of name is null, the value of "Unknown" is assigned to result using the null merge operator.

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 example above, since the value of name is null, the conditional operator is used to assign the value of "Unknown" to result.

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;
(length); // Output: null

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.

This is the article about the simplified assignment method of C# to NULL. For more related content of C# to NULL, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!