Null-Conditional Operators in C# (?. and ?[]) Last Updated : 01 Nov, 2025 Comments Improve Suggest changes Like Article Like Report Null-conditional operators in C# provide a concise and safe way to access members or elements of objects that might be null. They prevent NullReferenceException by short-circuiting the operation when the target object is null.C# provides two null-conditional operators:?. : Safely accesses a member (property, field, method or event) of an object that could be null.?[] : Safely accesses an element of an array or indexer when the object might be null.If the object is null, the expression evaluates to null instead of throwing an exception. 1. Using the ?. Operator C# using System; public class Student { public string Name { get; set; } public int? Age { get; set; } } class Program { static void Main() { Student s = null; // Safe member access string name = s?.Name; int? age = s?.Age; Console.WriteLine(name); Console.WriteLine(age); } } Output:(null)(null)Explanation:s?.Name checks whether s is not null before accessing the Name property.If s is null, the expression simply returns null instead of throwing an exception.You can combine it with the null-coalescing operator to provide a fallback value: C# int age = s?.Age ?? 18; // Returns 18 if s or s.Age is null The ?. operator can also be chained through multiple levels of object references: C# string city = s?.Address?.City; It can also be used with events for safe invocation without explicit null checks: C# EventHandler handler = null; handler?.Invoke(this, EventArgs.Empty); // Safe event call 2. Using the ?[] Operator C# using System; class Program { static void Main() { int[] numbers = null; // Safe array access int? firstNumber = numbers?[0]; Console.WriteLine(firstNumber); } } Ouptut:(null)Explanation:numbers?[0] tries to access the first element only if numbers is not null.If numbers is null, it returns null and avoids throwing a NullReferenceException.This operator is useful when working with arrays, lists or dictionaries that might not be initialized: C# List<string> names = null; string firstName = names?[0]; // Returns null safely 3. Combining Null-Conditional with Methods C# using System; class Program { static void Main() { string text = null; // Safe method invocation int? length = text?.Length; Console.WriteLine(length); } } Output:(null)Explanation:text?.Length safely checks whether text is not null before retrieving its length.This prevents runtime exceptions when working with potentially null objects or strings.Similar logic applies to method calls: C# int? count = text?.Trim()?.Length; Each operation executes only if the previous result is not null.Use CasesAccessing nested properties in complex models where intermediate objects can be null.Safely invoking methods or events on possibly uninitialized objects.Working with optional elements in arrays, lists or dictionaries.Simplifying null checks and improving code readability. Suggested Quiz Edit Quiz 3 Questions What is the main purpose of the null-conditional operator ?. in C#? A To throw an exception when the object is null B To safely access a member of an object that might be null C To convert value types to reference types D To automatically initialize null objects Explanation: What does the ?[] operator do? A Initializes an array with default values B Safely accesses an element of an array or indexer if the array might be null C Converts arrays to nullable types D Throws an exception if the array is null Explanation: The ?[] operator allows safe element access. If the array or collection is null, it returns null rather than throwing an exception, avoiding runtime errors. Which of the following statements is TRUE when using null-conditional operators? A You cannot chain multiple ?. operators B ?. can be combined with the null-coalescing operator ?? for fallback values C ?. automatically initializes the object if it is null D ?[] can only be used with arrays, not lists or dictionaries Explanation: Null-conditional operators can be combined with ?? to provide a default value if the result is null. Quiz Completed Successfully Your Score : 2/3 Accuracy : 0% Login to View Explanation 1/3 1/3 < Previous Next > Comment L lucky331w Follow 0 Improve L lucky331w Follow 0 Improve Article Tags : C# Explore IntroductionC# Tutorial 2 min read Introduction to .NET Framework 6 min read C# .NET Framework (Basic Architecture and Component Stack) 6 min read C# Hello World 2 min read Common Language Runtime (CLR) in C# 4 min read FundamentalsC# Identifiers 2 min read Data Types in C# 6 min read C# Variables 4 min read C# Literals 5 min read Operators in C# 7 min read C# Keywords 5 min read Control StatementsC# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch) 5 min read C# Switch Statement 4 min read Loops in C# 4 min read C# Jump Statements (Break, Continue, Goto, Return and Throw) 4 min read OOP ConceptsClass and Objects in C# 4 min read Constructors in C# 5 min read C# Inheritance 3 min read Encapsulation in C# 2 min read C# Abstraction 4 min read MethodsMethods in C# 4 min read Method Overloading in C# 4 min read Method Parameters in C# 4 min read Method Overriding in C# 7 min read Anonymous Method in C# 2 min read ArraysArrays in C# 6 min read Jagged Arrays in C# 4 min read Array Class in C# 5 min read How to Sort an Array in C# | Array.Sort() Method Set - 1 8 min read How to find the rank of an array in C# 2 min read ArrayListArrayList in C# 6 min read ArrayList Class in C# 4 min read C# | Array vs ArrayList 2 min read StringStrings in C# 6 min read C# Verbatim String Literal - @ 5 min read C# String Class 9 min read C# StringBuilder 2 min read C# String vs StringBuilder 3 min read TupleC# Tuple 7 min read C# Tuple Class 3 min read C# ValueTuple 7 min read C# ValueTuple Struct 4 min read IndexersC# Indexers 5 min read C# Multidimensional Indexers 5 min read C# - Overloading of Indexers 3 min read Like