I have a class like this.
public abstract class HtmlObject<T>
{
public HtmlObject() {}
public HtmlObject(string id, string name, T value)
{
this.ID = id;
this.Name = name;
this.Value = value;
}
public string ID { get; set; }
public string Name { get; set; }
public T Value { get; set; }
public abstract string Build();
}
With a concrete implementation that looks like this.
public class HtmlRadio : HtmlObject<string>
{
private const string RadioHtml = "<input type='radio' name='{0}' value='{1}' {2} />{1}<br />";
public bool Checked { get; set; }
public override string Build()
{
if (this.Checked)
return string.Format(HtmlRadio.RadioHtml, this.Name, this.Value, "checked='checked'");
else
return string.Format(HtmlRadio.RadioHtml, this.Name, this.Value, string.Empty);
}
}
And what I want to know is if the call to Build() would be safe if made across threads. My assumption is it wouldn’t because if I take the following series of calls
HtmlRadio radio = new HtmlRadio();
radio.Checked = false;
//Something could happen here?
string result = radio.Build();
My understanding is that the value of radio.Checked could change between it being set and the call to Build(), is this correct? If so how could I ‘fix’ this if I wanted to?
Is another thread able to access
radio? If not, then you are fine.In addition, what are you afraid of? If checked changes from false to true or true to false, do you really care? It’s not going to blow up – it will return a boolean, not throw an exception.
Edit: No, it is not thread-safe as written, another thread could change
CheckedandValueandName, none of which are protected in any way in any order.