Adding Different Item types to a C# Collection

Posted on 2004-10-29
Last Modified: 2010-04-15
I have a class in C# that inherits the Collectionbase with appropriate routines (code snippet below,

public class MyClass : System.Collections.CollectionBase
  //Add a Standard Segment Object
  public void Add(Segment xSegment)

  //Add a Loop Object
  public void Add(Loop xLoop)
 public Loop LoopItem(int Index)

 public Segment Item(int Index)

I have encountered an instance however, where I think it will be most efficient if I can have a collection class that can hold two different type of Objects.  I was able to override the add routine easily enough.  But as you can see I cannot override the "Get" item routine.  I have to have LoopItem and Item routines which causes problems later on.

Any ideas?


Question by:mandalorian4
    LVL 15

    Accepted Solution

    return an object

    public object Item(int Index)

    Then after you have it returned you can see what type of object it is and use it accordingly.
    LVL 3

    Author Comment


    Thank you! It was right in front of me!

    LVL 9

    Expert Comment

    Hi Mandalorian

    The problem you're experiencing is the following.....

    every method in C# has a method name, its parameters and the return type.

    Unfortunately, if the name and the parameters are the same, the compiler doesn't know which method the programmer is calling so as to know the type of the object to return..... it wouldn't know until runtime.

    Thats why methods called the same cannot differ only in the return type.

    Therefore as SRigney you should return an object and evaluate after which type of object you're receiving.

    Hope this helps


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    884 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

    16 Experts available now in Live!

    Get 1:1 Help Now