Question

I have an abstract class that is supposed to become the base of a huge hierarchy of classes. Among other things, it has a certain abstract method. During the execution, it will be called on all objects of this class constantly by a system that knows only about this abstract class and not it's children:

    foreach( AbstractClass object in allTheObjects )
        object.DoStuff();

However, a lot of it's children in fact don't override this method (which is empty in the base class); ones that use it and ones that don't a distributed among the class hierarchy. Will the C# take care of this empty method, or will I have to optimize it in some way?

P.S.: I know that premature optimization is evil, but it just got me really curious.

Was it helpful?

Solution

I wouldn't worry about that. In C#, method calls are really cheap. And even if you would optizmizie this, I doubt that you will see any differnce. See this post for reference.

I'd worry more about if you could avoid looping through everything, or what data strucutre allTheObjects is. I'd say there's much more potential for optimization there.

Also you might think about if you really need a big inheritance structure, or if you can achieve your goals with composition or interfaces.

You will also find more information here (interface methods vs. delegates vs. normal method calls)

OTHER TIPS

However, a lot of it's children in fact will have this method empty

Maybe you need to declare this method as virtual instead of abstract? Thus you can provide default empty implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top