type TSuit = (Clubs, Spades, Hearts, Diamonds); var CardCount: array[TSuit] of integer; ... CardCount[Clubs] := 13;
In .NET, however, you can not specify an enumerated type as the indexer into an array, so a line by line conversion to C# will not work. Instead, you need to create an entirely new helper class to make the calling code more readable (i.e. without putting casting code in all of the uses of the indexer). Furthermore, you can't use the code below in a Compact Framework (CF) application, because Enum.GetValues() and Enum.GetNames() aren't implemented in the CF libraries.
class Program{ static void Main(string[] args) { CardCounter cc = new CardCounter(); cc[Suit.Clubs] = 13; }} public enum Suit {Clubs, Spades, Hearts, Diamonds}; public class CardCounter{ private static int enumCount = Enum.GetValues(typeof(Suit)).Length; private int[] counts = new int[enumCount]; public int this[Suit suit] { get { return counts[(int)suit]; } set { counts[(int)suit] = value; } }}
class Program
{
static void Main(string[] args)
CardCounter cc = new CardCounter();
cc[Suit.Clubs] = 13;
}
public enum Suit {Clubs, Spades, Hearts, Diamonds};
public class CardCounter
private static int enumCount = Enum.GetValues(typeof(Suit)).Length;
private int[] counts = new int[enumCount];
public int this[Suit suit]
get { return counts[(int)suit]; }
set { counts[(int)suit] = value; }
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.