I need to enforce an interface AND have base functionality in C#

Posted on 2009-02-09
Last Modified: 2013-12-17
I need to force child classes to override certain virtual functions and have a certain amount of functionality in the base class.

Since I need member variables and function bodies, I can not use an abstract class.  Therefore I can not use an abstract method.  Then, since the best I can do is virtual functions, those can be overridden optionally in the child classes.

What I think need is a pure virtual function.  Am I right?

How do I implement this in the base class?  I am having trouble using the format I expected to use and have seen on the web:

public virtual void DisplayCancelOpMessage() = 0;

I get a "; expected" error from the compiler.

Thanks for the help,

Question by:newbieweb
    LVL 8

    Accepted Solution

    >> Since I need member variables and function bodies, I can not use an abstract class.

    I tihnk you mean you can't use an interface.  You can do both of these things in an abstract class.
    LVL 18

    Expert Comment

    by:Richard Lee
    A pure virtual function in c# is as follows:

    public abstract void DisplayCancelOpMessage();

    How i normally solve your dilemna is having an interface which is implemented by an abstract class then provide a body for the methods or properties i need to implement then declare the others as abstract / pure virtual if you prefer this C++ term.
    LVL 26

    Assisted Solution

    by:Anurag Thakur
    to implement your requirement >> I need to enforce an interface AND have base functionality in C#
    abstract class provides a perfect solution
    enforce the interface by providing pure virtual functions and base functionality support is already there in the abstract class

    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

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 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

    18 Experts available now in Live!

    Get 1:1 Help Now