I have a base class that has a virtual property:
public virtual string Name { get; set; }
then I have a derived class that overrides only the getter of the property
public override string Name
{
get { return "Fixed Name"; }
}
The problem is this only overrides the getter. If someone calls the setter the base class setter will get called, and the caller won’t know that it’s ineffective.
So I thought I’d do something like:
public override string Name
{
get { return "Fixed Name"; }
set { throw new Exception(); } //which exception type?
}
So two (related questions):
- Is there a better pattern for me to use?
- If I should be using the above pattern, what exception to use?
Edit: Some reasons why one exception would be preferred over another would be good. My co-worker and I have had the same argument between NotSupported and InvalidOperation.
Throw NotSupportedException exception.
Quote from the link:
My opinion is that InvalidOperationException is not a correct option.
Quote from MSDN:
There is nothing about current state in your situation. It is that the class contract does not support the operation.