I start learning C# couple of weeks ago and I can't figure out how to declare a public const char array.
This is what I have:
public static class MyClass { public const char[] MyCharArray= new char[]{'a','b','x','c'}; } The error is pretty clear, however I have not found a way to fix this:
'MyClass.MyCharArray' must be constant
It needs to be const because I need to use it as a argument for custom data annotation validation. Ex: [MyCustumValidation(MyClass.MyCharArray)]
public static readonly char[] MyCharArray= new char[]{'a','b','x','c'};readonlywill stop people from doingMyClass.MyCharArray = new[] { ... }, but won't stop them from doingMyClass.MyCharArray[0] = ...MyCustomValidation(MyCustomValidator), that is, pass a type that performs the validation. Alternatively, you could simply use a string to represent a collection of characters -- a slight abuse of the mechanism but it would allow you to stick with attributes.