How can I create a generic collection which contains a specific type?

Jack

In C# I would like to create a generic deck of cards. Enabling me to e.g. Create a stack of cards, a queue of cards or any other collection of cards? Providing that this collection is a derived from IEnumerable.

public class Deck<T> where T : IEnumerable
{
    private T<ICard> __Cards;

    public Deck() : this(52){}

    public Deck(int cards)
    {
        __Cards = new T<Card>(cards);
    }
}

Some other class... calling

Deck<List> _Deck = new Deck<List>();

I get the following error:

The type parameter 'T' cannot be used with type arguments
JaredPar

I think this is a case that would be better solved with inheritance

public abstract class Deck 
{
  public abstract ICard this[int index]
  {
    get;
    set;
  }

  protected void Create(int cardCount);
}

public sealed class DeckList : Deck
{
  private List<ICard> m_list;
  public override ICard this[int index] 
  {
    get { return m_list[index]; }
    set { m_list[index] = value; }
  }
  protected override void Create(int cards) 
  {
    m_list = new List<ICard>(cards);
  }
}

If you continue down the generic path I think you will ultimately find that you need a type with 2 generic parameters

  • The collection type
  • The element type

Example

public class Deck<TElement, TCollection> 
  where TCollection : IEnumerable<TElement>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I to count files are which contains a specific artist?

From Dev

Returning generic type which is a specific collection based on type passed to parameter

From Dev

Returning generic type which is a specific collection based on type passed to parameter

From Dev

How can I specialize a method for a specific generic type?

From Dev

How make a collection of type which is generic type in c#

From Dev

How can I create a collection of the same type as a record in a table?

From Dev

How do I also specify a generic collection type for a generic class?

From Dev

how to create a collection of a type that has a generic value type argument

From Dev

How can I add a reminder which contains a specific application in Reminders on Mac?

From Dev

Can I use a class with a generic type to a collection of a different class?

From Dev

How do I create a Factory for a generic type?

From Dev

How do I create a Factory for a generic type?

From Dev

How do I create an instance of a generic type?

From Dev

Wordpress: How can I create an external page or file that contains posts with 1 specific category?

From Dev

how can i determine the ACTUAL return type of a specific subclass of a generic superclass?

From Dev

Why can I not cast to a generic interface from a type with a generic argument which is a subtype of the generic argument in my interface?

From Dev

How can I get a generic type annotation

From Dev

How can I create a generic setter for JTextField?

From Dev

How can I create a generic setter for JTextField?

From Dev

How to delete a line which contains a specific syntax but I want to avoid a line which contains another word

From Dev

how can I create a type which behaves just like another type?

From Dev

How can I create a type hint that my returned list contains strings?

From Dev

How can I create a generic list monoid in scala that maintains the inner type of the list involved?

From Dev

How can I create an interface for a collection of objects?

From Dev

How can I pass generic type to generic methode?

From Dev

How can I create three random integers which sum to a specific value? (Python)

From Dev

How can I create three random integers which sum to a specific value? (Python)

From Dev

How can I create a function which accepts a function with specific parameters as an argument?

From Dev

How to create generic type?

Related Related

  1. 1

    How can I to count files are which contains a specific artist?

  2. 2

    Returning generic type which is a specific collection based on type passed to parameter

  3. 3

    Returning generic type which is a specific collection based on type passed to parameter

  4. 4

    How can I specialize a method for a specific generic type?

  5. 5

    How make a collection of type which is generic type in c#

  6. 6

    How can I create a collection of the same type as a record in a table?

  7. 7

    How do I also specify a generic collection type for a generic class?

  8. 8

    how to create a collection of a type that has a generic value type argument

  9. 9

    How can I add a reminder which contains a specific application in Reminders on Mac?

  10. 10

    Can I use a class with a generic type to a collection of a different class?

  11. 11

    How do I create a Factory for a generic type?

  12. 12

    How do I create a Factory for a generic type?

  13. 13

    How do I create an instance of a generic type?

  14. 14

    Wordpress: How can I create an external page or file that contains posts with 1 specific category?

  15. 15

    how can i determine the ACTUAL return type of a specific subclass of a generic superclass?

  16. 16

    Why can I not cast to a generic interface from a type with a generic argument which is a subtype of the generic argument in my interface?

  17. 17

    How can I get a generic type annotation

  18. 18

    How can I create a generic setter for JTextField?

  19. 19

    How can I create a generic setter for JTextField?

  20. 20

    How to delete a line which contains a specific syntax but I want to avoid a line which contains another word

  21. 21

    how can I create a type which behaves just like another type?

  22. 22

    How can I create a type hint that my returned list contains strings?

  23. 23

    How can I create a generic list monoid in scala that maintains the inner type of the list involved?

  24. 24

    How can I create an interface for a collection of objects?

  25. 25

    How can I pass generic type to generic methode?

  26. 26

    How can I create three random integers which sum to a specific value? (Python)

  27. 27

    How can I create three random integers which sum to a specific value? (Python)

  28. 28

    How can I create a function which accepts a function with specific parameters as an argument?

  29. 29

    How to create generic type?

HotTag

Archive