Solved

Should a virtual method in a abstract base class throw?

Posted on 2013-07-01
1
492 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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