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

I used the DefaultValue attribute in a code generator that writes a C # class definition from a schema.

I am stuck where the property in the schema is an array of strings.

I would like to write something like this in my C #:

[DefaultValue(typeof(string[]), ["a","b"])]
public string[] Names{get;set;}

but it will not compile.

Is it possible to successfully declare a default attribute for a string array?

+5
source share
1 answer

You can try

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

As you want to pass a new array of strings, you need to instantiate it - done new string[]. C # resolves an initialization list with the initial contents of the array, which follows in braces, i.e. { "a", "b" }.

+9
source

All Articles