How can I initialize an const / static array of structs as clearly as possible?

class SomeClass
{

    struct MyStruct
    {
        public string label;
        public int id;
    };

    const MyStruct[] MyArray = {
          {"a", 1}
          {"b", 5}
          {"q", 29}
    };
};
share|improve this question

4 Answers

up vote 32 down vote accepted

Firstly, do you really have to have a mutable struct? They're evil. Likewise public fields.

Other than that, I'd just create a constructor taking the two bits of data:

class SomeClass
{

    struct MyStruct
    {
        private readonly string label;
        private readonly int id;

        public MyStruct (string label, int id)
        {
            this.label = label;
            this.id = id;
        }

        public string Label { get { return label; } }
        public string Id { get { return id; } }

    }

    static readonly IList<MyStruct> MyArray = new ReadOnlyCollection<MyStruct>
        (new[] {
             new MyStruct ("a", 1),
             new MyStruct ("b", 5),
             new MyStruct ("q", 29)
        });
}

Note the use of ReadOnlyCollection instead of the array itself - this will make it immutable, avoiding the problem exposing arrays directly.

share|improve this answer
2  
@Chris: No, I'd say it goes far beyond just publicly consumable APIs. How big is your team? Are you always going to work for that company? How sure are you that every member of your team, now and forever, understands the many and varied oddnesses of mutable structs? For throwaway code, I'd agree... but most code lives much longer in maintenance mode than we originally expect, and poor design can make it much harder to disentangle that later on. You find that someone's passed a field by reference and then you can't refactor to properties without breaking it, etc. – Jon Skeet Mar 1 '11 at 6:26
Awesome John, you just made me realize a new angle of modern object oriented programming. Thank you.. – Adarsha Mar 2 '12 at 4:43

Are you using C# 3.0? You can use object initializers like so:

static MyStruct[] myArray = 
            new MyStruct[]{
                new MyStruct() { id = 1, label = "1" },
                new MyStruct() { id = 2, label = "2" },
                new MyStruct() { id = 3, label = "3" }
            };
share|improve this answer
3  
With C# 3 you don't need the () for each constructor call (as you're using an object initializer) and you can ditch the "new MyStruct[]" bit as that's implied anyway due to being the initializer for an array variable. – Jon Skeet Nov 21 '08 at 17:35

You cannot initialize reference types by default other than null. You have to make them readonly. So this could work;

    readonly MyStruct[] MyArray = new MyStruct[]{
      new MyStruct{ label = "a", id = 1},
      new MyStruct{ label = "b", id = 5},
      new MyStruct{ label = "c", id = 1}
    };
share|improve this answer
4  
Note that that only makes the variable readonly - it doesn't prevent other code from replacing elements in the array. – Jon Skeet Nov 21 '08 at 17:36

I'd use a static constructor on the class that sets the value of a static readonly array.

public class SomeClass
{
   public readonly MyStruct[] myArray;

   public static SomeClass()
   {
      myArray = { {"foo", "bar"},
                  {"boo", "far"}};
   }
}
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.