In C# I would like to create a generic deck of cards. Enabling me to eg 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
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
Example
public class Deck<TElement, TCollection>
where TCollection : IEnumerable<TElement>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.