Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 434503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:16:52+00:00 2026-05-12T20:16:52+00:00

So I have been intrigued by the ?? operator, but have still been unable

  • 0

So I have been intrigued by the ?? operator, but have still been unable to use it. I usually think about it when I am doing something like:

var x = (someObject as someType).someMember;

If someObject is valid and someMember is null, I could do

var x = (someObject as someType).someMember ?? defaultValue;

but almost invariably I get into problems when someObject is null, and ?? doesn’t help me make this any cleaner than doing the null check myself.

What uses have you guys found for ?? in practical situations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:16:52+00:00Added an answer on May 12, 2026 at 8:16 pm

    I agree with you that the ?? operator is usually of limited use– it’s useful to provide a fall-back value if something is null, but isn’t useful to prevent a Null Reference Exception in cases when you want to continue drilling down into properties or methods of a sometimes-null reference.

    IMHO, what’s needed much more than ?? is a “null-dereference” operator which allows you to chain long property and/or method chains together, e.g. a.b().c.d().e without having to test each intermediate step for null-ness. The Groovy language has a Safe Navigation operator and it’s very handy.

    Luckily, it seems that the C# team is aware of this feature gap. See this connect.microsoft.com suggestion for the C# team to add a null-dereference operator to the C# language.

    We get quite a number of requests for
    features similar to this one. The “?.”
    version mentioned in the community
    discussion is closest to our hearts –
    it allows you to test for null at
    every “dot”, and composes nicely with
    the existing ?? operator:

    a?.b?.c ?? d

    Meaning if any of a , a.b or a.b.c is
    null use d instead.

    We are considering this for a future
    release, but it won’t be in C# 4.0.

    Thanks again,

    Mads Torgersen, C# Language PM

    If you also want this feature in C#, add your votes to the suggestion on the connect site! 🙂

    One workaround I use to get around the lack of this feature is an extension method like what’s described in this blog post, to allow code like this:

    string s = h.MetaData
                .NullSafe(meta => meta.GetExtendedName())
                .NullSafe(s => s.Trim().ToLower())
    

    This code either returns h.MetaData.GetExtendedName().Trim().ToLower() or it returns null if h, h.MetaData, or h.MetaData.GetExtendedName() is null. I’ve also extended this to check for null or empty strings, or null or empty collections. Here’s code I use to define these extension methods:

    public static class NullSafeExtensions
    {
        /// <summary>
        /// Tests for null objects without re-computing values or assigning temporary variables.  Similar to 
        /// Groovy's "safe-dereference" operator .? which returns null if the object is null, and de-references
        /// if the object is not null.
        /// </summary>
        /// <typeparam name="TResult">resulting type of the expression</typeparam>
        /// <typeparam name="TCheck">type of object to check for null</typeparam>
        /// <param name="check">value to check for null</param>
        /// <param name="valueIfNotNull">delegate to compute if check is not null</param>
        /// <returns>null if check is null, the delegate's results otherwise</returns>
        public static TResult NullSafe<TCheck, TResult>(this TCheck check, Func<TCheck, TResult> valueIfNotNull)
            where TResult : class
            where TCheck : class
        {
            return check == null ? null : valueIfNotNull(check);
        }
    
        /// <summary>
        /// Tests for null/empty strings without re-computing values or assigning temporary variables
        /// </summary>
        /// <typeparam name="TResult">resulting type of the expression</typeparam>
        /// <param name="check">value to check for null</param>
        /// <param name="valueIfNotNullOrEmpty">delegate to compute non-null value</param>
        /// <returns>null if check is null, the delegate's results otherwise</returns>
        public static TResult CheckNullOrEmpty<TResult>(this string check, Func<string, TResult> valueIfNotNullOrEmpty)
            where TResult : class
        {
            return string.IsNullOrEmpty(check) ? null : valueIfNotNullOrEmpty(check);
        }
        /// <summary>
        /// Tests for null/empty collections without re-computing values or assigning temporary variables
        /// </summary>
        /// <typeparam name="TResult">resulting type of the expression</typeparam>
        /// <typeparam name="TCheck">type of collection or array to check</typeparam>
        /// <param name="check">value to check for null</param>
        /// <param name="valueIfNotNullOrEmpty">delegate to compute non-null value</param>
        /// <returns>null if check is null, the delegate's results otherwise</returns>
        public static TResult CheckNullOrEmpty<TCheck, TResult>(this TCheck check, Func<TCheck, TResult> valueIfNotNullOrEmpty)
            where TCheck : ICollection
            where TResult : class
        {
            return (check == null || check.Count == 0) ? null : valueIfNotNullOrEmpty(check);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been perpetually intrigued by test-driven development, but I can never follow through
have been searching for a mod operator in ios, just like the % in
Have been scatching my head about this - and I reckon it's simple but
Have been working in Ruby for a while, but usually in the context of
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
Have been reading about async and tasks and been attempting to convert the CopyFileEx
Have been searching how to convert a dictionary to a string. But the results
I have been using TortoiseSVN for some time and I really like it. I
I've been intrigued by all the android world since I first learned about it
I've only just recently learned about Google's programming language, Go. I've been intrigued by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.