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 3668406
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:07:07+00:00 2026-05-19T02:07:07+00:00

So this is the meat of the question: Can Foo.Bar ever return null? To

  • 0

So this is the meat of the question: Can Foo.Bar ever return null? To clarify, can ‘_bar’ be set to null after it’s evaluated as non-null and before it’s value is returned?

    public class Foo
    {
        Object _bar;
        public Object Bar
        {
            get { return _bar ?? new Object(); }
            set { _bar = value; }
        }
    }

I know using the following get method is not safe, and can return a null value:

            get { return _bar != null ? _bar : new Object(); }

UPDATE:

Another way to look at the same problem, this example might be more clear:

        public static T GetValue<T>(ref T value) where T : class, new()
        {
            return value ?? new T();
        }

And again asking can GetValue(…) ever return null? Depending on your definition this may or may not be thread-safe… I guess the right problem statement is asking if it is an atomic operation on value… David Yaw has defined the question best by saying is the above function the equivalent to the following:

        public static T GetValue<T>(ref T value) where T : class, new()
        {
            T result = value;
            if (result != null)
                return result;
            else
                return new T();
        }
  • 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-19T02:07:07+00:00Added an answer on May 19, 2026 at 2:07 am

    No, this is not thread safe.

    The IL for the above compiles to:

    .method public hidebysig specialname instance object get_Bar() cil managed
    {
        .maxstack 2
        .locals init (
            [0] object CS$1$0000)
        L_0000: nop 
        L_0001: ldarg.0 
        L_0002: ldfld object ConsoleApplication1.Program/MainClass::_bar
        L_0007: dup 
        L_0008: brtrue.s L_0010
        L_000a: pop 
        L_000b: newobj instance void [mscorlib]System.Object::.ctor()
        L_0010: stloc.0 
        L_0011: br.s L_0013
        L_0013: ldloc.0 
        L_0014: ret 
    }
    

    This effectively does a load of the _bar field, then checks its existence, and jumps ot the end. There is no synchronization in place, and since this is multiple IL instructions, it’s possible for a secondary thread to cause a race condition – causing the returned object to differ from the one set.

    It’s much better to handle lazy instantiation via Lazy<T>. That provides a thread-safe, lazy instantiation pattern. Granted, the above code is not doing lazy instantiation (rather returning a new object every time until some time when _bar is set), but I suspect that’s a bug, and not the intended behavior.

    In addition, Lazy<T> makes setting difficult.

    To duplicate the above behavior in a thread-safe manner would require explicit synchronization.


    As to your update:

    The getter for the Bar property could never return null.

    Looking at the IL above, it _bar (via ldfld), then does a check to see if that object is not null using brtrue.s. If the object is not null, it jumps, copies the value of _bar from the execution stack to a local via stloc.0, and returns – returning _bar with a real value.

    If _bar was unset, then it will pop it off the execution stack, and create a new object, which then gets stored and returned.

    Either case prevents a null value from being returned. However, again, I wouldn’t consider this thread-safe in general, since it’s possible that a call to set happening at the same time as a call to get can cause different objects to be returned, and it’s a race condition as which object instance gets returned (the set value, or a new object).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked this question yesterday, looking if more people can help me out. I
I asked this question yesterday got a pretty good answer but can't figure out
After viewing answer of almost a same question on SO i added this line
This is a bit of a long shot, but if anyone can figure it
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw
I am very new to programming, so please forgive me if this question seems
This is an attempt to rephrase my previous question as a result of the
My question is very similar to this question but a bit more specific. My
Maybe this is a stupid question but it's bugging me. I have a bi-directional

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.