Solved

Should a virtual method in a abstract base class throw?

Posted on 2013-07-01
1
495 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 36

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 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