"C#: The Mystery of the Question Mark After Variables"

Understanding the C# Question Mark after Variable

In C#, the question mark (?) is a powerful tool that can significantly enhance your code's readability and maintainability. It's used in a specific context, after a variable, to indicate null-conditional operators and null-coalescing operators. This article will delve into these operators, their usage, and their benefits.

Null-Conditional Operators

Introduced in C# 6.0, null-conditional operators allow you to safely navigate through references that may be null. The question mark (?) is used to replace the dot (.) when calling methods or accessing properties. If the reference is null, the entire expression evaluates to null, preventing a NullReferenceException.

Syntax: object?.Method() or object?.Property

??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ???

Example

Consider the following code without null-conditional operators:

string result = person.Name.ToUpper();

If person or person.Name is null, this will throw a NullReferenceException. With null-conditional operators, you can safely write:

string result = person?.Name?.ToUpper();

Null-Coalescing Operators

Null-coalescing operators provide a shorthand way to specify a default value if a variable is null. The question mark (?) is used after the variable, and the value to use if the variable is null follows it.

来自 Illus_man 的照片和图片组合 | Shutterstock 投稿者
来自 Illus_man 的照片和图片组合 | Shutterstock 投稿者

Syntax: variable ?? defaultValue

Example

Without null-coalescing operators, you might write:

string result = person != null ? person.Name : "No name";

With null-coalescing operators, you can simplify this to:

many question marks are shown in white on black
many question marks are shown in white on black

string result = person?.Name ?? "No name";

Null-Forgiving Operators

In C# 8.0, the null-conditional and null-coalescing operators were combined to create null-forgiving operators. The exclamation mark (!) is used to assert that a null value will not occur. This can be useful when you know that a value cannot be null, but the compiler cannot prove it.

Syntax: expression!

Example

Consider the following code with a potential null warning:

string result = person.Name.ToUpper();

You can use the null-forgiving operator to assert that person.Name will not be null:

string result = person!.Name.ToUpper();

Best Practices

  • Use null-conditional operators to make your code safer and more readable.
  • Prefer null-coalescing operators over ternary operators for default value assignment.
  • Use null-forgiving operators sparingly and only when you're certain that a null value will not occur.

By understanding and effectively using these operators, you can write cleaner, more maintainable, and less error-prone C# code.

a group of people sitting at desks in an office with a question mark above them
a group of people sitting at desks in an office with a question mark above them
an illuminated question mark in the dark
an illuminated question mark in the dark
a blue question mark on a dark background
a blue question mark on a dark background
What
What
two black and white images with question marks
two black and white images with question marks
A message to the aliens, part 23/23 (wat)
A message to the aliens, part 23/23 (wat)
Abstract Question Mark Digital Art Animation -  Blue and Orange Digital Lights & Bokeh Background
Abstract Question Mark Digital Art Animation - Blue and Orange Digital Lights & Bokeh Background
Creative Brainstorming Ideas, Person Thinking With Question Mark, Question Mark Design Inspiration, Red Question Mark Graphic, Transparent Background Question Mark, Animated Question Marks, Confusion Png, Icons For Questions, Png Animated
Creative Brainstorming Ideas, Person Thinking With Question Mark, Question Mark Design Inspiration, Red Question Mark Graphic, Transparent Background Question Mark, Animated Question Marks, Confusion Png, Icons For Questions, Png Animated
Question Mark
Question Mark
typewriter question mark symbol
typewriter question mark symbol
a blue question mark on a white background
a blue question mark on a white background
Question and exclamation Mark Seamless Pattern Background
Question and exclamation Mark Seamless Pattern Background
사마귀에 대한 오해와 진실, Yes or No - 하이닥
사마귀에 대한 오해와 진실, Yes or No - 하이닥
Computer Icons Symbol Question Mark PNG
Computer Icons Symbol Question Mark PNG
a question mark in black and blue on a white background with the letter s below it
a question mark in black and blue on a white background with the letter s below it
an orange question mark on a transparent background png - question mark clipart images
an orange question mark on a transparent background png - question mark clipart images
many white question marks on a black background
many white question marks on a black background
#questionmark Red Question Mark Illustration, Cartoon Question Mark Design, Red Question Marks Design, Red Question Mark Logo, Quetion Mark, Red Question Mark Sticker, Red Question Mark Graphic, Question Mark Graphic, Red Question Mark
#questionmark Red Question Mark Illustration, Cartoon Question Mark Design, Red Question Marks Design, Red Question Mark Logo, Quetion Mark, Red Question Mark Sticker, Red Question Mark Graphic, Question Mark Graphic, Red Question Mark
a red question mark on a white background png, transparent and no background image
a red question mark on a white background png, transparent and no background image
Guys this is me when my teacher says “do u guys get it?” And I’m like NO (btw this is my first post)
Guys this is me when my teacher says “do u guys get it?” And I’m like NO (btw this is my first post)
Question mark
Question mark
Yellow Circle Question Mark Icon PNG & SVG Design For T-Shirts
Yellow Circle Question Mark Icon PNG & SVG Design For T-Shirts