Make sure base method gets called in C#
Posted
by Fnatte
on Stack Overflow
See other posts from Stack Overflow
or by Fnatte
Published on 2010-05-31T17:45:18Z
Indexed on
2010/05/31
17:53 UTC
Read the original article
Hit count: 201
Can I somehow force a derived class to always call the overridden methods base?
public class BaseClass
{
public virtual void Update()
{
if(condition)
{
throw new Exception("..."); // Prevent derived method to be called
}
}
}
And then in a derived class :
public override void Update()
{
base.Update(); // Forced call
// Do any work
}
I've searched and found a suggestion to use a non-virtual Update() but also a protected virtual UpdateEx(). It just doesn't feel very neat, isn't there any better way?
I hope you get the question and I am sorry for any bad English.
© Stack Overflow or respective owner