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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:57:08+00:00 2026-05-18T06:57:08+00:00

I would like to have thread-safe read and write access to an auto-implemented property.

  • 0

I would like to have thread-safe read and write access to an auto-implemented property. I am missing this functionality from the C#/.NET framework, even in it’s latest version.
At best, I would expect something like

[Threadsafe]
public int? MyProperty { get; set; }

I am aware that there are various code examples to achieve this, but I just wanted to be sure that this is still not possible using .NET framework methods only, before implementing something myself. Am I wrong?

EDIT: As some answers elaborate on atomicity, I want to state that I just want to have that, as far as I understand it: As long as (and not longer than) one thread is reading the value of the property, no other thread is allowed to change the value. So, multi-threading would not introduce invalid values. I chose the int? type because that is the on I am currently concerned about.

EDIT2: I have found the specific answer to the example with Nullable here, by Eric Lippert

  • 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-18T06:57:08+00:00Added an answer on May 18, 2026 at 6:57 am

    Correct; there is no such device. Presumably you are trying to protect against reading the field while another thread has changed half of it (atomicity)? Note that many (small) primitives are inherently safe from this type of threading issue:

    5.5 Atomicity of variable references

    Reads and writes of the following data
    types are atomic: bool, char, byte,
    sbyte, short, ushort, uint, int,
    float, and reference types. In
    addition, reads and writes of enum
    types with an underlying type in the
    previous list are also atomic.

    But in all honesty this is just the tip of the threading ice-berg; by itself it usually isn’t enough to just have a thread-safe property; most times the scope of a synchronized block must be more than just one read/write.

    There are also so many different ways of making something thread-safe, depending on the access profile;

    • lock ?
    • ReaderWriterLockSlim ?
    • reference-swapping to some class (essentially a Box<T>, so a Box<int?> in this case)
    • Interlocked (in all the guises)
    • volatile (in some scenarios; it isn’t a magic wand…)
    • etc

    (not to mention making it immutable (either through code, or by just choosing not to mutate it, which is often the simplest way of making it thread-safe)

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

Sidebar

Related Questions

I have a thread safe observable collection replacement which I would like to write
How would I go about this? I have 3 rows like so: ID THREAD
I have a thread pool (executor) which I would like to monitor for excessive
I would like to create a thread pool. I have a class called ServerThread.cpp,
I would like to have an AppWidget that designed like this one . Image:
I would like to implement a thread safe queue in VS 6.0 Is there
I would like two threads work like this: First thread will append values to
Let's say we have a thread-safe compare-and-swap function like long CAS(long * Dest ,long
I have input, 34600 milliseconds I would like to output this in the format
So in my project i would like have a nice treeview that has images.

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.