How to Resolve Error "CS0134: A const field of a reference type other than string can only be initialized with null" in C#
The Compiler Error CS0134 is a type system restriction error. The message reads: "'Variable' is of type 'Type'. A const field of a reference type other than string can only be initialized with null."
In C#, a const field must have a value that is fully determined at compile-time. Most reference types (classes, arrays, lists) require the new keyword to create an instance. The new operator allocates memory on the heap, which is a runtime operation. Since the compiler cannot predict the memory address of an object before the program runs, it forbids assigning object instances to constants.
This guide explains why this restriction exists and how to use static readonly to achieve similar behavior for objects.
Understanding Compile-Time vs. Runtime Values
- Compile-Time (
const): The value is baked directly into the Intermediate Language (IL) code. This works for primitives (int,bool,double) and string literals because their data is embedded in the binary. - Runtime (
new): Creating a class instance (likenew User()) asks the Operating System for memory RAM. The compiler cannot know what that memory address will be ahead of time.
Because const fields are essentially hardcoded replacements, you cannot hardcode a memory allocation request.
Scenario: Attempting to create an Object in a const
This error commonly occurs when developers want a global, immutable instance of a list, array, or custom object.
Example of error: trying to declare a "constant" list or object using the new keyword.
using System.Collections.Generic;
public class GameSettings
{
// ⛔️ Error CS0134: 'GameSettings.DefaultPlayers' is of type 'List<string>'.
// A const field of a reference type other than string can only be initialized with null.
public const List<string> DefaultPlayers = new List<string> { "Alice", "Bob" };
// ⛔️ Error CS0134: Applies to arrays too
public const int[] Numbers = new int[] { 1, 2, 3 };
// ⛔️ Error CS0134: Applies to custom classes
public const Person Admin = new Person("Admin");
}
public class Person
{
public Person(string n) { }
}
Solution: Use static readonly
The direct equivalent of a const for reference types is static readonly.
static: The field belongs to the class (shared), just like a const.readonly: The field can only be assigned during initialization or in a static constructor.
This allows the value to be calculated at runtime (when the program starts), satisfying the need for new, while ensuring the variable cannot be reassigned later.
Solution: replace const with static readonly.
using System.Collections.Generic;
public class GameSettings
{
// ✅ Correct: Initialized at runtime, but cannot be reassigned.
public static readonly List<string> DefaultPlayers = new List<string> { "Alice", "Bob" };
// ✅ Correct: Array initialization
public static readonly int[] Numbers = new int[] { 1, 2, 3 };
// ✅ Correct: Custom object initialization
public static readonly Person Admin = new Person("Admin");
}
Immutability Note: static readonly prevents you from replacing the list itself (DefaultPlayers = new List... will fail). However, it does not prevent you from modifying the contents of the list (DefaultPlayers.Add("Charlie") works). If you need a truly immutable list, use System.Collections.ObjectModel.ReadOnlyCollection or ImmutableList.
Exceptions: Strings and Null
The error message specifically mentions: "other than string". Why are strings allowed?
Strings in C# are reference types, but they have special support called String Interning. String literals (like "Hello") are stored in a special metadata table during compilation. This allows the compiler to treat them like constants.
Similarly, null is a fixed value (conceptually a zero pointer) known at compile-time.
public class Configuration
{
// ✅ Valid: Strings are the only reference type allowed as const
public const string AppName = "MySuperApp";
// ✅ Valid: Null is a compile-time constant
public const object MissingData = null;
}
Conclusion
CS0134 prevents you from asking the compiler to do the impossible (predicting runtime memory allocation).
- Identify the Type: Is it a class, array, or list?
- Check the Keyword: Are you using
const? - Apply the Fix: Change
consttostatic readonly. This preserves the "shared and write-once" semantics while allowing the use of thenewkeyword.