2

In C#, you can type:

int[] ia = new int[] { 1, 2, 3, 4 };

And it will initialize an array of ints, of Length 4, containing those numbers.

I have a class Card, representing a Card in a deck, and I want to be able to say something like:

Card[] ca = new Card[] { "4S", "5C", "AH" };

And have an array of Cards, the first Card being the 4 of Spades, then the 5 of Clubs, then the Ace of Hearts.

I already can do:

Card c = new Card("4S");

And have a Card representing the 4 of Spades.

Is this constructor type even possible? If possible, how would I create it?


PS: I know I could go:

Card[] ca = Cards.Parse("4S 5C AH");

Or something lame like that, but I'm hoping I can avoid that, and use prettier syntax.

Now that I've typed this all out, I feel a bit sheepish. I'm "that guy" now... #Guilty

1
  • Take a look at implicit casting. This would make the code convert a string to a Card automatically. In general its bad practice but I find it useful sometimes. msdn.microsoft.com/en-us/library/z5z9kes2.aspx Commented Jan 2, 2015 at 17:25

2 Answers 2

6

The usual way:

var ca = new Card[] { new Card( "4S" ), new Card( "5C" ), new Card( "AH" ) };

Explicit cast:

class Card
{
    ...
    public static explicit operator Card( string s )
    {
        return new Card( s );
    }
    ...
}

var ca = new Card[] { (Card)"4S", (Card)"5C", (Card)"AH" };

Implicit cast:

class Card
{
    ...
    public static implicit operator Card( string s )
    {
        return new Card( s );
    }
    ...
}

var ca = new Card[] { "4S", "5C", "AH" };

Now, whether this is nice or not is up to you to decide. I'd also think about if the card class should use strings or not. Enums might be nicer, and it'd potentially save some validation and parsing.

Sign up to request clarification or add additional context in comments.

1 Comment

The only reason I'm using strings is that I'm solving this Euler Problem: projecteuler.net/problem=54
4

Implicit casting might be a solution to this but I would recommend doing it the clean way by providing the ctors:

var ca = new Card[] { new Card("4S"), new Card("5C"), new Card("AH") };

Is there a problem with this approach?

A somewhat shorter approach would be letting the compiler figure out the type:

var ca = new [] { new Card("4S"), new Card("5C"), new Card("AH") };

3 Comments

Note these are called "Collection Initializers" or sometimes "List initializers" msdn.microsoft.com/en-us/library/bb384062.aspx
@Samuel: Your solution does work, but there's a lot of characters, and if I'm working with this class for a while, it's going to get tedious. But, your suggestion to use implicit casting worked beautifully!
@AaronLS: Thank you for the link, now I'll stop initializing my List<int> objects with: List<int> li = new List<int>(new int[] {1,2,3}); And instead use: List<int> li = new List<int>{1,2,3}; Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.