In C++, if I want an object to be initialized at compile time and never change thereafter, I simply add the prefix const
.
In C#, I write
// file extensions of interest
private const List<string> _ExtensionsOfInterest = new List<string>()
{
".doc", ".docx", ".pdf", ".png", ".jpg"
};
and get the error
A const field of a reference type other than string can only be initialized with null
Then I research the error on Stack Overflow and the proposed "solution" is to use ReadOnlyCollection<T>
. A const field of a reference type other than string can only be initialized with null Error
But that doesn't really give me the behavior I want, because
// file extensions of interest
private static ReadOnlyCollection<string> _ExtensionsOfInterest = new ReadOnlyCollection<string>()
{
".doc", ".docx", ".pdf", ".png", ".jpg"
};
can still be reassigned.
So how do I do what I'm trying to do?
(It's amazing how C# has every language feature imgaginable, except the ones I want)