In C#, you can define a const string, but not an array as arrays are objects. It is to my understanding that strings are in fact objects as they are reference objects passed by value just like arrays.
So how is it that we can do this:
const string NewLine = "\r\n";
but not this:
const byte[] AesSwapBytes = new byte[] { ... };
Is it because we can’t change individual characters on strings (NewLine[0] = '\n'), but can on arrays (arr[0] = i)?
Whether or not you can make a variable has nothing to do with whether it’s an object, or a struct. What is required to make a variable
constis that the right hand size of the assignment must be a compile time literal. There are only a handful of types that have compile time literals.stringis one, as isint,double, and the other numeric types. As was mentioned in another answer,nullis a compile time literal, so if you really wanted to you could assign any nullable type to beconstand assign null to it (not that it would really be useful). If C# were to add compile time literals (other than null) that resulted in arrays then you could create a meaningfulconstarray. Until then, you’ll be stuck using some other mechanisms to do what you want.