Syntax

C# Null-Conditional Operators Syntax

In C#, null-conditional operators: safely reads values that might be null — var len = name?.Length ?? 0;

Quick Answer

Syntax
var len = name?.Length ?? 0;
C#Syntax

Null-Conditional Operators

Code example
var len = name?.Length ?? 0;

Safely reads values that might be null.

Learn More

How to null-conditional operators in C#

  1. Open your C# file or editor.
  2. Copy the example, then replace its sample names and values for your code.

C# Null-Conditional Operators Syntax

Use var len = name?.Length ?? 0;.

Related Syntax References

Frequently asked questions

What is the C# syntax for null-conditional operators?

Use this pattern: var len = name?.Length ?? 0;

Can I copy this C# code example?

Yes. Use Copy code and adapt the example to your program.

Is this C# syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify