Todd Gerbert
asked on
Thread Safety .Net C#
Just for the sake of my own edification...I'd appreciate your thoughts.
is the following thread-safe?
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; }
}
}
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you all for contributing, very helpful!
(It would seem strings are not quite as immutable as I was led to believe!)
(It would seem strings are not quite as immutable as I was led to believe!)
> 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 --
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
-- Abel --
unsafe Thread might be when there is more than one thread.