zzg
zzg

Reputation: 101

C# assignment thread safe

Is the following assignment code thread safe?

public Stream Stream
{
    get
    {
        return stream ?? (stream = new NetworkStream(Socket));
    }
    set
    {
        stream = value;
    }
}

Upvotes: 0

Views: 57

Answers (1)

EricSchaefer
EricSchaefer

Reputation: 26330

No. It it entirely possible that set setter is called, after stream is evaluated (being null), but before the new NetworkStream is assigned. This would mean that the newly set stream (via the setter) is overwritten with the new NetworkStream.

Upvotes: 2

Related Questions