What operators does C# provide to handle null values?

WBOY
Release: 2023-09-23 17:57:02
forward
1161 people have browsed it

C# 提供哪些运算符来处理空值?

C# has the following three operators to handle null values ​​-

Null coalescing operator (??)

allows you to get the value of a variable (if not) null, or specify a default value that can be used.

It replaces the following expression in C# -

string resultOne = value != null ? value : "default_value";
Copy after login

with the following expression -

string resultTwo = value ?? "default_value";
Copy after login

Here is an example illustrating this.

Example

using System;
class Program{
   static void Main(){
      string input = null;
      string choice = input ?? "default_choice";
      Console.WriteLine(choice); // default_choice
      string finalChoice = choice ?? "not_chosen";
      Console.WriteLine(finalChoice); // default_choice
   }
}
Copy after login

Null coalescing assignment operator (??=)

If the value on the left is not null, return the value. Otherwise, it returns the value on the right. In other words, it allows you to initialize a variable to some default value if its current value is null.

It replaces the following expression in C# -

if (result == null)
result = "default_value";
Copy after login

Use the following expression.

result ??= "default_value";
Copy after login

This operator is useful for lazily computed properties. For example -

Example

class Tax{
   private Report _lengthyReport;
   public Report LengthyReport => _lengthyReport ??= CalculateLengthyReport();
   private Report CalculateLengthyReport(){
      return new Report();
   }
}
Copy after login

Null Conditional Operator (?.)

This operator allows you to safely call methods on an instance. If the instance is null, return null instead of throwing a NullReferenceException. Otherwise, it just calls the method.

It replaces the following expression in C# -

string result = instance == null ? null : instance.Method();
Copy after login

Use the following expression -

string result = instance?.Method();
Copy after login

Consider the following example.

Example

using System;
string input = null;
string result = input?.ToString();
Console.WriteLine(result); // prints nothing (null)
Copy after login

Example

Real-time demonstration

using System;
class Program{
   static void Main(){
      string input = null;
      string choice = input ?? "default_choice";
      Console.WriteLine(choice); // default_choice
      string finalChoice = choice ?? "not_chosen";
      Console.WriteLine(finalChoice); // default_choice
      string foo = null;
      string answer = foo?.ToString();
      Console.WriteLine(answer); // prints nothing (null)
   }
}
Copy after login

Output

default_choice
default_choice
Copy after login

The above is the detailed content of What operators does C# provide to handle null values?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template