Avatar of Todd Gerbert
Todd GerbertFlag for United States of America

asked on 

Thread Safety .Net C#

Just for the sake of my own edification...I'd appreciate your thoughts.

is the following thread-safe?
public class MyClass
{
	private static readonly int _intValue = 123;
	private static readonly string _stringValue = "one two three";
 
	static MyClass() // Empty static constructor
	{
	}
 
	public MyClass()
	{
                     // instance init
	}
 
	public int IntValue
	{
		get { return _intValue; }
	}
 
	public string StringValue
	{
		get { return _stringValue; }
	}
}

Open in new window

.NET ProgrammingC#

Avatar of undefined
Last Comment
abel
SOLUTION
Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of jjamstrong
jjamstrong
Flag of Honduras image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of SaedSalman
SaedSalman
Flag of Jordan image

There is only one thraed in your code. there will not be a cross-thread nor deadlock nor race condition.
unsafe Thread might be when there is more than one thread.
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

ASKER

Saed, the question is what if I created a multi-threaded application that created instances of the above class would access to the private static members, via the public properties, be safe?
SOLUTION
Avatar of SaedSalman
SaedSalman
Flag of Jordan image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

ASKER

Thank you all for contributing, very helpful!

(It would seem strings are not quite as immutable as I was led to believe!)
Avatar of abel
abel
Flag of Netherlands image

> Thank you all for contributing, very helpful!

you're welcome, glad we could be of some help :)

> (It would seem strings are not quite as immutable as I was led to believe!)

they are indeed not immutable, nor is a private/internal/protected field/method/property really private/internal/protected...

-- Abel --
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo