C++では、オブジェクトをコンパイル時に初期化してから変更しない場合は、接頭辞const
を追加するだけです。C#で "const"と同等のものを得るにはどうしたらいいですか?
C#では、私は
// file extensions of interest
private const List<string> _ExtensionsOfInterest = new List<string>()
{
".doc", ".docx", ".pdf", ".png", ".jpg"
};
を作成し、文字列以外のレファレンスタイプのCONSTフィールドのみ がNULL
で初期化することができるエラーを
を得ますスタックオーバーフローに関するエラーを調べ、提案された "解決策"は
ReadOnlyCollection<T>
を使用することです。 A const field of a reference type other than string can only be initialized with null Errorしかし
// file extensions of interest private static ReadOnlyCollection<string> _ExtensionsOfInterest = new ReadOnlyCollection<string>() { ".doc", ".docx", ".pdf", ".png", ".jpg" };
はまだがを再割り当てすることができますので、それは本当に、私が欲しいの挙動を与えるものではありません。
私は何をしようとしていますか?
(これは、C#は私が欲しいものを除き、すべての言語機能imgaginableを持っているか驚くべきことだ)
プライベート静的読み取り専用ReadOnlyCollectionあなたは答えを発見し、それの重要な点を(それがヒントが含まれています)欠場します。学習は難しい。 – Sinatr