Skip to main content

How to Resolve Error "CS0283: The type 'type' cannot be declared const" in C#

The Compiler Error CS0283 is a type restriction error. The message reads: "The type 'Type' cannot be declared const".

In C#, the const keyword creates a Compile-Time Constant. This means the compiler must be able to calculate the value and embed it directly into the assembly metadata at build time. Because complex types (like classes, structs, arrays, and DateTime) require memory allocation at Runtime (via the new operator), they cannot be used as constants.

This guide explains which types support const and how to use static readonly for complex objects.

Understanding Const Limitations

The C# compiler limits const usage to types where the value can be fully represented as a literal in the binary code.

Allowed Types:

  • Primitives: byte, sbyte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool.
  • Strings: string literals.
  • Enums: Any enumeration type.
  • Null: Any reference type can be const if and only if the value is null.

Disallowed Types (Triggers CS0283):

  • Structs: Custom structs (e.g., Point, DateTime, TimeSpan).
  • Classes: Any user-defined class (unless value is null).
  • Arrays: int[], string[].

Scenario 1: Structs and DateTime

A common mistake is assuming that because DateTime or Color (struct) represents a fixed value, it can be a constant. However, creating a DateTime requires a constructor call, which happens at runtime.

Example of error:

using System;

public class AppConfig
{
// ⛔️ Error CS0283: The type 'DateTime' cannot be declared const.
// The compiler cannot execute 'new DateTime(...)' during the build.
public const DateTime ReleaseDate = new DateTime(2023, 1, 1);

// ⛔️ Error CS0283: Custom structs are also forbidden.
public const Point Origin = new Point(0, 0);
}

public struct Point
{
public int X, Y;
public Point(int x, int y) { X = x; Y = y; }
}

Scenario 2: Arrays and Collections

Arrays are objects that live on the heap. Even if you know the exact contents of the array while writing code, the act of allocating memory for that array is a runtime operation.

Example of error:

public class Constants
{
// ⛔️ Error CS0283: The type 'int[]' cannot be declared const.
public const int[] ValidIds = { 1, 2, 3 };
}

Solution: Use static readonly

The direct replacement for const when dealing with objects, structs, or arrays is static readonly.

  • static: The field is shared by the class (like a constant), not specific to an instance.
  • readonly: The field cannot be reassigned after initialization.

This moves the initialization from Compile-Time to Start-Up Time (when the class is first loaded), allowing the use of new, math, or logic.

Solution: replace const with static readonly.

using System;

public class AppConfig
{
// ✅ Correct: Initialized once at runtime.
public static readonly DateTime ReleaseDate = new DateTime(2023, 1, 1);

// ✅ Correct: Arrays work perfectly here.
public static readonly int[] ValidIds = { 1, 2, 3 };
}

public class Program
{
static void Main()
{
Console.WriteLine(AppConfig.ReleaseDate.Year);
}
}
note

Immutability Warning: static readonly prevents you from replacing the object (e.g., ValidIds = new int[]... is blocked). However, for reference types like arrays, it does not prevent changing the content (e.g., ValidIds[0] = 99 is allowed). For true immutability, use ImmutableArray or ReadOnlyCollection.

Conclusion

CS0283 prevents you from treating dynamic objects as static literals.

  1. Check the Type: Is it a number, string, or bool? If not, const is likely invalid.
  2. Check the Value: Is the value null? If yes, const is allowed for classes. If no, const is forbidden.
  3. The Fix: Simply change const to static readonly. This provides the same "fixed value" behavior but allows for object instantiation.