[Last Call] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2009-02-14
Medium Priority
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

Accepted Solution

AndreSteffens earned 500 total points
ID: 23644330
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

Author Closing Comment

ID: 31546938
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.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

831 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