Solved

Should a virtual method in a abstract base class throw?

Posted on 2013-07-01
1
489 Views
Last Modified: 2013-07-01
My abstract base class implements an interface and a method like so:

public interface IFoo
{
       void Bar();
}

public abstract class Foo : IFoo
{
      public virtual void Bar()
      {
          throw new NotImplementedException();
      }
}

Since the base class has no default implementation of this method and I can't make the method abstract, is it reasonable to let the base implementation throw or should it just return? What is the common approach to this? Thanks!
0
Comment
Question by:farooqam
1 Comment
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 39291829
Based on your needs, 3 alternatives:
A1) If you are not implementing it in the base class, you should implements IFoo interface in the sub classes not the base class.
A2) If you do need to define a Bar method in the abstract class , then do not implement IFoo interface and all your sub classes will have the option toimplement Bar if required.
A3) Preferred one. Define the Bar method as abstract:
public abstract void Bar();
All your subclasses are forced to implement it.
Check:
http://www.dotnetperls.com/abstract
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now