How do I declare a DefaultValue attribute whose value is an array of strings?

You could try

[DefaultValue(new string[] { "a", "b" })]

As you want to pass a new string array, you have to instantiate it – that’s done by new string[]. C# allows an initialization list with the initial contents of the array to follow in braces, i.e. { "a", "b" }.


EDIT: As correctly pointed out by Cory-G, you may want to make sure your actual instances do not receive the very array instance stored in the DefaultValue attribute. Otherwise, changes to that instance might influence default values across your application.

Instead, you could use your property’s setter to copy the assigned array.

Leave a Comment