My code requires a field of my class to be set but I want the field to be set only once. If the developer tries to reset it/change it, I would ideally like the compiler to tell us off instead of getting a run time error. Is this possible?
Code to help explain
internal class Message
{
private string title = string.Empty;
public string Title
{
get { return title; }
set
{
if (string.IsNullOrEmpty(title))
title = value;
else
throw new Exception("Title can only be set once!");
}
}
}
As you can see, the above will throw an exception but this is a run time error. Although the example here is fairly trivial the concept of writing compiler error or warning messages could be very beneficial.