Home > Backend Development > C++ > How Does the C# 6.0 Null-Conditional Operator (?. ) Simplify Null Handling?

How Does the C# 6.0 Null-Conditional Operator (?. ) Simplify Null Handling?

Patricia Arquette
Release: 2025-01-25 15:26:08
Original
694 people have browsed it

How Does the C# 6.0 Null-Conditional Operator (?. ) Simplify Null Handling?

C# 6.0?.. Operator: Simplified air value processing

C# 6.0 introduced?. The operator brought great convenience to the air value processing. This operator is usually called the empty conditional operator, which provides a simple and secure way to access the attributes or calling methods of the empty object.

Let's take a look at the following code fragment:

Let's break down the working principle of this operator:
public class A
{
    public string PropertyOfA { get; set; }
}

...

var a = new A();
var foo = "bar";
if (a?.PropertyOfA != foo)
{
    //somecode
}
Copy after login

It first evaluates the first operation number (A) and checks whether it is empty. If A is empty, the entire expression will be evaluated as NULL and will not be further evaluated.
  • If A is not empty, continue to evaluate the visit of the second operation (Propertyofa) as a member of A. In this example, it accesss A's propertyofa property.
  • The result of the expression is either the value of the NULL (if A is empty) or the value of A.propertyofa.
  • In essence, the operational symbols act as a measure of preventing abnormal reference abnormalities. It allows you to access the attributes or call methods of the empty object, not to cause the application to collapse.

Consider this alternative grammar:

Although this method has reached the same result, it involves separate air values ​​and conditional assignments. ?. The operator provides a more concise and elegant solution.
string bar = (a == null ? null : a.PropertyOfA);
if (bar != foo)
{
    ...
}
Copy after login

In addition, the operation symbol can change the type of expression. For example, FileInfo.Length is a long attribute. However, if you use it on it?. Operator:

You finally got a Long? Type expression, where the question mark indicates that the value may be empty. This eliminates the demand for explicit vacuum inspection, simplifies your code and improves its availability.
FileInfo fi = ...; // fi 可能为空
long? length = fi?.Length; // 如果 fi 为空,length 将为 null
Copy after login

The above is the detailed content of How Does the C# 6.0 Null-Conditional Operator (?. ) Simplify Null Handling?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template