开发者

How to initialize an array constant specifying desired indexes

What I just want is to initialize a string[] array constant by specifying not only the values, but the indexes they will be attached to.

For exa开发者_运维百科mple, on:

private static readonly string[] Pets = new string[] {"Bulldog", "GreyHound"};

I would like to state that BullDog corresponds to index 29 and GreyHound to 5 (Like php :) )

Any suggestion?

Cheers,


If you have some flexibility in terms of your data structure, it would be more efficient to use a Dictionary<int, string> instead of an array for this behavior.

Example (if you are using C# 3 or above):

var pets = new Dictionary<int, string> {
    { 29, "Bulldog" },
    { 5, "Greyhound" }
};
Console.WriteLine(pets[5]);

Same example for legacy applications:

Dictionary<int, string> pets = new Dictionary<int, string>();
pets[29] = "Bulldog";
pets[5] = "Greyhound";
Console.WriteLine(pets[5]);


It sounds like you don't want an array, but a Dictionary<int, string> instead, which could be initialized like this:

private static readonly Dictionary<int, string> pets = 
    new Dictionary<int, string> {
    { 29, "Bulldog" },
    { 5, "Greyhound" }
};

(Note that this collection initializer syntax was only added in C# 3. If you're using an older version you'll have to call Add or the indexer explicitly multiple times.)

You can access a dictionary via its indexer which looks like array access:

string x = pets[29];
pets[10] = "Goldfish";


I don't think what you want is possible in C# when declaring arrays.

Besides using a Dictionary as others have suggested, you could try using an enumeration instead, with values corresponding to your specific array indices and descriptions (using the Description attribute) corresponding to the string values.

private enum Pets
{
   [Description("GreyHound")]
   Greyhound = 5,
   [Description("Bulldog")]
   Bulldog = 29
}


For the record, I agree with everyone that a Dictionary is probably more appropriate. But you can write a little method to pull off what you want:

public static T[] CreateArray<T>(params Tuple<int, T>[] values)
{
    var sortedValues = values.OrderBy(t => t.Item1);

    T[] array = new T[sortedValues.Last().Item1 + 1];

    foreach(var value in sortedValues)
    {
        array[value.Item1] = value.Item2;
    }

    return array;
}

And call it like this:

string[] myArray = CreateArray(new Tuple<int, string>(34, "cat"), new Tuple<int, string>(12, "dog"));

If C# receives the syntactic sugar for Tuple that many people seem to want, this would get a tad cleaner looking.

Is this a good idea? Almost certainly not, but I'll leave that for the OP to judge.


You can not do that in the initializer, you need to first specify the size of the array and then add items at specific locations.

private static readonly string[] Pets = new string[42];

and then in a static constructor you insert your items.

private static MyClass
{
    Pets[29] = "Bulldog";
    Pets[5] = "Greyhound";
}

But as other have suggested: use the Dictionary<int, string>.


You don't need a string array, but instead a Dictionary.

Take a look at link text, there's a fine example there (which I adapted here):

Dictionary<int, string> d = new Dictionary<int, string>();
        d.Add(2, "cat");
        d.Add(1, "dog");
        d.Add(0, "llama");
        d.Add(-1, "iguana");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜