Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6849857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:02:53+00:00 2026-05-27T01:02:53+00:00

I’m working on a library to use in a card game. I spent a

  • 0

I’m working on a library to use in a card game. I spent a couple of hours learning indexers so I could have a Deck class that functions like an array of Card types. Then I achieved the same thing and a whole lot more by inheriting from ArrayList. I therefore ask why one would go to the trouble of making a class indexable when the same thing (and much more) can be achieved via “: ArrayList”.

I think my question is pretty self explanatory, but for those who like to look at code.

Here is my Deck class that contains an indexer:

class DeckIndexer
{
    private Card[] myData;
    private int arrSize;



public DeckIndexer(int size)
{
    arrSize = size;
    myData = new Card[size];

    for (int i = 0; i < size; i++)
    {
        myData[i] = null;
    }
}

public Card this[int pos]
{
    get
    {
        return myData[pos];
    }
    set
    {
        myData[pos] = value;
    }
}

public Card this[Card c]
{
    get
    {
        int count = 0;

        for (int i = 0; i < arrSize; i++)
        {
            if (myData[i] == c)
            {
                count++;
            }
        }
        return c;
    }
    set
    {
        for (int i = 0; i < arrSize; i++)
        {
            if (myData[i] == c)
            {
                myData[i] = value;
            }
        }
    }
  }
}

Now here is the second solution, inheriting from ArrayList

  class DeckInheritence : ArrayList
  {

  }

The second solution gives me 20+ methods for free, [] syntax, and saves me however many lines of code. The first gives me [] syntax, the methods attached to object, and is hard to implement.

BTW, yes I can see that ArrayList is giving me an array of objects, not cards, but there is not much fudging to get it to require a Card type.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:02:53+00:00Added an answer on May 27, 2026 at 1:02 am

    Firstly, unless you are in .NET 1.1, List<Card> would be preferable. However, as for inheriting… if all you are doing is representing a list of cards, I would just use List<Card> (with neither encapsulation nor inheritance).

    As for the why; encapsulation is useful when being a list is not the primary purpose of a type, but it is useful for the caller to have access to a few methods like indexers. As Jon notes, inheriting doesn’t (for example) give you any control over what the user does to your data.

    Personally, I don’t recommend inheriting from List<T> (or ArrayList); that binds an implementation detail into the actual type declaration (changing inheritance is a breaking change).

    If you want to expose lots of methods with minimal work, all you need to do is provide IEnumerable<T> (as an interface) – then all of LINQ becomes available. So if you need something bespoke (more than List<Card>) my advice would be:

    • encapsulate List<T> (i.e. a private field)
    • implement IEnumerable<T> if appropriate (just return theList.GetEnumerator())
    • add any specific members you want (indexers etc)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.