As a heads up, I'm learning C# at the moment and going through a textbook when I ran into this obstacle.
How do you call ElementAt
from an IEnumerable<T>
?
The second comment in this
SO question mentions it, but I just get an error.
Here they mention doing it as well, but they don't tell you how!
In case I'm missing something basic, here's my code:
using System.Collections.Generic;
class Card {}
class Deck
{
public ICollection<Card> Cards { get; private set; }
public Card this[int index]
{
get { return Cards.ElementAt(index); }
}
}
I've resorted to this from the information I got on the MSDN Library page:
class Deck
{
public ICollection<Card> Cards { get; private set; }
public Card this[int index]
{
get {
return System.Linq.Enumerable.ElementAt<Card>(Cards, index);
}
}
}
All this comes from the section on collections and how the second code implementation I showed makes it easier to grab a specific element from the list rather than having to iterate through the enumerator.
Deck deck = new Deck();
Card card = deck[0];
Instead of:
Deck deck = new Deck();
Card c1 = null;
foreach (Card card in deck.Cards){
if (condition for the index)
c1 = card;
}
Am I doing this right or am I missing something? Thanks for any input!