C# - Extend, inherit, contain, or what?

Posted on 2009-02-14
Last Modified: 2013-11-07
I have a network of nodes and edges, each edge connecting to exactly two nodes, and each node having zero to many edges. That's my base data which several programs will load.

Next, one particular program will want to store screen coordinates with the nodes, and a series of Points for the edges, plus a line style to draw the edges in. I don't want this in the base data as it's not relevant to other programs, so it needs to be in seperate class of some kind. Another program might want to store 3d coordinates for the nodes rather than just 2d, and have superelevation on the edges. Again, this is superfluous data that I don't want in the base class.

As far as I can tell there are three main ways of doing so:
class LatLonNode { public BaseNode node } (containment)
class LatLonNode : BaseNode (inheritance)
class LatLonNode { public DoSomething(this BaseNode bn) {} } (extension)

Extension looks confusing and can it have properties? Containment looks messy. Inheritance, from past experience, would normally be the way to go but I'm trying to learn new techniques!

Any recommendations for/against any particular method would be appreciated. Thanks.
Question by:gmayo
    LVL 4

    Accepted Solution

    There is no fundamental difference between inheritance and extension. Say you want to add a new method to an exisiting class. Using inheritance you do it like  this:
    public class BaseClass

    public NewClass:BaseClass
      public void NewMethod()

    using extension you do it like this:
    public static class MyExtensions
      public static void NewMethod(this BaseClass base)

    using extension you can only add methods to a type; no fields, or properties.


    for more details
    LVL 8

    Author Closing Comment

    I think, despite my earlier reservations, that containment fits the way I need to go. I should have said that there are other factors, namely RTTI, that doesn't work with inheritance or extension. But thanks anyway.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Article by: Nadia
    Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now