Reputation: 101
Is the following assignment code thread safe?
public Stream Stream
{
get
{
return stream ?? (stream = new NetworkStream(Socket));
}
set
{
stream = value;
}
}
Upvotes: 0
Views: 57
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