Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

?.: practical reference

?.: practical reference: ?. and ?[] stop a continuous access chain at null and return null.

Version: C# operator reference; verified against C# 14

Syntax

receiver?.member / receiver?[index]

?. and ?[] stop a continuous access chain at null and return null.

Use them for genuinely optional members, combining with ?? for a default.

They do not hide exceptions from a getter or method, and parentheses can end the short-circuit chain.

For a non-null receiver, ordinary member or indexer access or a method call occurs, so its getter, indexer, or method can throw; a value-type result becomes nullable.

The complete program actually uses ?.; its exact expected stdout is True.

Examples

using System;
using System.Linq;
using System.Threading.Tasks;

string? value = null; Console.WriteLine(value?.Length is null);

Result

True

The complete program actually uses `?.`; its exact expected stdout is `True`.

Sources