Skip to content

ErcanOPAK.com

  • ASP.Net WebForms
  • ASP.Net MVC
  • C#
  • SQL
  • MySQL
  • PHP
  • Devexpress
  • Reportviewer
  • About
C#

The Null Conditional Operator in C# (?.)

- 13.12.22 - ErcanOPAK

The null conditional operator (?.) is colloquially referred to as the “Elvis operator” because of its resemblance to a pair of dark eyes under a large quiff of hair. The null conditional is a form of a member access operator (the .).

Here’s a simplified explanation for the null conditional operator:

The expression A?.B evaluates to B if the left operand (A) is non-null; otherwise, it evaluates to null.

Many more details fully define the behavior:

  • The type of the expression A?.B is the type of B, in cases where B is a reference type. If B is a value type, the expression A?.B is the nullable type that wraps the underlying value type represented by B.
  • The specification for the feature mandates that A be evaluated no more than once.
  • The null conditional operator short-circuits, which means that you can chain multiple ?. operators, knowing that the first null encountered prevents the remaining (rightmost) components of the expression from being evaluated.

Let’s look at some examples to explain those behaviors. Consider this simplified Car class:

public class Car
{
    public string Model { get; set; }
    public int Speed { get; set; }
    public int FuelType { get; set; }
}

Assume that c represents a car. Consider these two statements:

var model = c?.Model;
var speed = c?.Speed;

The variable model is a string. The value of model depends on the value of c. If c is null, model is null. If c is not null, model is the value of c.Model. Note that c.Model may be null even when c is not.

The variable speed is an int? (which is another way of specifying a Nullable<int>). As with speed, the value of speed depends on the value of c. If c is null, speed is an int? with no value. If c is non-null, speed is the wrapped value of c.Speed.

That’s the basics. The power of this feature comes from all the scenarios where this feature enables cleaner code.

Related posts:

What is the difference between 'ref' and 'out' keywords in C#
Convert comma separated string into a List in C#
How to set delivery format while using SMTP client in C#?
How to add placeholder to Multiple Selection DropDownList in Asp.Net MVC
Post Views: 1

Post navigation

The Null-Coalescing Operators in C# (?? and ??=)
The Ternary Operator in C# (?:)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

January 2023
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
« Dec    

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (512)
  • Add Constraint to SQL Table to ensure email contains @ (288)
  • Get the User Name and Domain Name from an Email Address in SQL (274)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (263)
  • Find numbers with more than two decimal places in SQL (246)
  • ASPxGridView – Disable CheckBox based on condition in GridViewCommandColumn (230)
  • Confirm before process with ASPxButton in Devexpress (229)
  • How to solve “Response.Redirect cannot be called in a Page callback” for DevExpress Components (221)
  • How to make some specific word(s) Bold or Underline in ReportViewer (205)
  • Devexpress ASPxGridview Column Grouping in Code (205)

Recent Posts

  • What is the difference between ‘ref’ and ‘out’ keywords in C#
  • How to check if javascript is enabled on the client’s browser
  • How to disable ASP.Net button after click to prevent double clicking
  • What is the difference between HashSet and List in .net?
  • What is the purpose of nameof in C#?
  • What is the difference between Select and SelectMany in Linq
  • Differences between FirstOrDefault and SingleOrDefault in LINQ
  • How to reset identity seed after deleting records in SQL
  • The Ternary Operator in C# (?:)
  • The Null Conditional Operator in C# (?.)

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (512)
  • Add Constraint to SQL Table to ensure email contains @ (288)
  • Get the User Name and Domain Name from an Email Address in SQL (274)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (263)
  • Find numbers with more than two decimal places in SQL (246)

Recent Posts

  • What is the difference between ‘ref’ and ‘out’ keywords in C#
  • How to check if javascript is enabled on the client’s browser
  • How to disable ASP.Net button after click to prevent double clicking
  • What is the difference between HashSet and List in .net?
  • What is the purpose of nameof in C#?

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter

© 2023 ErcanOPAK.com

Proudly powered by WordPress | Theme: Xblog Plus by wpthemespace.com