How to Resolve Error "CS0031: Constant value 'value' cannot be converted to a 'type'" in C#
The Compiler Error CS0031 is a Range Overflow error. The message states: "Constant value 'value' cannot be converted to a 'type'".
This error occurs when you attempt to assign a constant value (like a literal number or a const variable) to a variable type that is not capable of holding that specific value. Usually, this means the number is too large for the target type (e.g., trying to fit 300 into a byte) or has the wrong sign (e.g., trying to assign -1 to an uint).
Because the value is constant, the compiler detects this problem immediately during the build, preventing a potential runtime crash or data corruption.
Understanding Type Ranges
Every numeric type in C# has specific limits. If a constant value falls outside these limits, CS0031 is triggered.
| Type | Min Value | Max Value |
|---|---|---|
byte | 0 | 255 |
sbyte | -128 | 127 |
short | -32,768 | 32,767 |
int | -2,147,483,648 | 2,147,483,647 |
uint (Unsigned) | 0 | 4,294,967,295 |
Scenario 1: Value Too Large (Overflow)
The most common cause is assigning a number literal that exceeds the maximum value of the target variable.
Example of Mistake
Trying to assign 300 to a byte (which maxes out at 255).
public void ConfigureSettings()
{
// ⛔️ Error CS0031: Constant value '300' cannot be converted to a 'byte'.
// A byte only has 8 bits (0 to 255). 300 requires 9 bits.
byte value = 300;
}
Solution: Use a Larger Type
Change the variable type to something that can hold the value, such as int or short.
public void ConfigureSettings()
{
// ✅ Correct: 'int' can easily hold 300.
int value = 300;
// Alternatively, if you must use a byte, correct the value:
byte smallValue = 255;
}
If you specifically want the value to "wrap around" (overflow) rather than fail (e.g., 300 becomes 44 in a byte), you must use the unchecked context and an explicit cast: byte b = unchecked((byte)300);.
Scenario 2: Sign Mismatch (Negative to Unsigned)
C# supports Unsigned types (uint, ulong, ushort, byte). These types interpret all bits as positive magnitude. Assigning a negative literal to them is invalid.
Example of Mistake
public void ProcessIndex()
{
// ⛔️ Error CS0031: Constant value '-1' cannot be converted to a 'uint'.
// 'uint' stands for Unsigned Integer. It cannot store negative numbers.
uint index = -1;
}
Solution
Either use a signed type (like int) or assign a valid positive number.
public void ProcessIndex()
{
// ✅ Correct: Use 'int' if you need negative numbers
int index = -1;
// ✅ Correct: Or assign a valid positive literal
uint positiveIndex = 1;
}
Scenario 3: Enum Underlying Types
By default, C# Enums are backed by int. However, you can change the backing type to byte or short. If you define an Enum member with a value that doesn't fit the backing type, CS0031 occurs.
Example of Mistake
// We declared this Enum to use 'byte' (Max 255)
public enum Status : byte
{
Start = 1,
Processing = 100,
// ⛔️ Error CS0031: Constant value '999' cannot be converted to a 'byte'
Error = 999
}
Solution
Change the backing type of the Enum to accommodate the larger values.
// ✅ Correct: 'short' (Max 32,767) can hold 999.
// Or simply remove ': short' to default to 'int'.
public enum Status : short
{
Start = 1,
Processing = 100,
Error = 999
}
Conclusion
CS0031 is the compiler telling you that the math doesn't fit.
- Check Limits: Verify the Min/Max values of the type you are assigning to.
- Check Signs: Ensure you aren't putting negative numbers into
uint,ulong, orbyte. - Change Types: If the value is correct but the type is too small, upgrade the variable (e.g.,
byte->int).