3 ms·
> This is an ironic statement given that your code, as written, is circumvented by subclassing Range and side-stepping its constructor. You are the second one
by thomasz 11y ago
> This is an ironic statement given that your code, as written, is circumvented by subclassing Range and side-stepping its constructor.
You are the second one who says this. I must admit that I do not understand how this would be possible. The properties are not virtual, and not writable for anything but the base constructor. To my knowledge, it's impossible avoid the base classes constructor by subclassing. I'm pretty sure you can actually circumvent the assert, but you would have to use the heavy weapons in the reflection API.
class Range
{
public int From { get; }
public int To { get; }
public Range(int from, int to)
{
Assert(from <= to);
From = from; To = to;
}
}
class LolRange : Range
{
public LolRange() : base(0,1)
{
From = 1; To = 0;
}
}
Error CS0200 Property or indexer 'Program.Range.From' cannot be assigned to -- it is read only
Error CS0200 Property or indexer 'Program.Range.To' cannot be assigned to -- it is read only