Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Should a virtual method in a abstract base class throw?

Posted on 2013-07-01
1
Medium Priority
?
507 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 36

Accepted Solution

by:
Miguel Oz earned 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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