C# > Core C# > Operators and Expressions > Null-conditional Operator (?.)
Basic Null-Conditional Operator Usage
This code snippet demonstrates the basic use of the null-conditional operator (?.) to safely access members of a potentially null object, avoiding NullReferenceExceptions.
Code Example
The code defines classes `Address` and `Person`. The `person` object is initialized to `null`. The line `string street = person?.Address?.Street;` uses the null-conditional operator. If `person` is null, the entire expression evaluates to null. If `person` is not null, it proceeds to access `person.Address`. If `person.Address` is also null, the expression evaluates to null. Only if both `person` and `person.Address` are not null, it accesses `person.Address.Street`. The `??` operator provides a default value if `street` is null, preventing a `NullReferenceException` when writing to the console.
using System;
public class Address
{
public string Street { get; set; }
}
public class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
public class Example
{
public static void Main(string[] args)
{
Person person = null; // Or could be initialized with a Person object.
// Without null-conditional operator, this could throw a NullReferenceException
// string street = person.Address.Street;
// Using the null-conditional operator
string street = person?.Address?.Street;
Console.WriteLine($"Street: {street ?? \"Address is unknown\"}");
}
}
Concepts Behind the Snippet
The null-conditional operator (?.) is a syntactic sugar provided by C# to simplify null checks. It allows you to access members and call methods of an object only if the object is not null. If the object is null, the expression short-circuits and evaluates to null without throwing an exception. This greatly reduces the verbosity and improves the readability of code that handles potentially null objects. It's equivalent to a series of `if (object != null)` checks.
Real-Life Use Case
Consider a scenario where you're fetching data from a database and the data might be incomplete. For example, a user might not have provided their address. Using the null-conditional operator, you can safely access the address details without worrying about NullReferenceExceptions. This is common in web applications where user profiles or data transfer objects (DTOs) might contain optional or missing information.
Best Practices
Always use the null-conditional operator when dealing with objects that might be null, especially when accessing nested properties. Combine it with the null-coalescing operator (??) to provide default values for properties that might be null, ensuring a smooth user experience and preventing unexpected errors. Avoid overusing it in situations where the object *should not* be null. In those cases, consider throwing an exception to indicate a programming error.
Interview Tip
Be prepared to explain how the null-conditional operator works and the benefits it provides over traditional null checks. Understand when to use it and when it might be more appropriate to throw an exception or handle the null case differently. Also, be ready to compare it with other related operators like the null-coalescing operator (??) and the null-coalescing assignment operator (??=).
When to Use Them
Use the null-conditional operator when accessing properties or methods of an object that might be null, particularly when dealing with nested object structures. It's especially helpful in scenarios where you want to avoid NullReferenceExceptions and provide a graceful way to handle missing data.
Alternatives
The traditional alternative is to use `if` statements to check for null before accessing members. For instance: csharp if (person != null && person.Address != null) { street = person.Address.Street; } else { street = null; // Or some default value } This is more verbose and less readable than using the null-conditional operator.
Pros
Cons
FAQ
-
What happens if I use the null-conditional operator on a value type?
Value types (like `int`, `bool`, `struct`) cannot be null directly. To use the null-conditional operator with a value type, you need to use a nullable value type (e.g., `int?`, `bool?`). -
Can I use the null-conditional operator to invoke methods?
Yes, you can. For example: `person?.DoSomething();` will call `DoSomething()` only if `person` is not null. If `DoSomething()` returns a value, you can chain further null-conditional operators.