Compiler Error CS0283
The type 'type' cannot be declared const
The type specified in a constant declaration must be byte
, sbyte
, ushort
, short
, uint
, int
, ulong
, long
, char
, float
, double
, decimal
, bool
, string
, an enum type, or a reference type that is assigned a value of null
. Each constant expression must yield a value of the target type or of a type that is implicitly convertible to the target type.
Example
The following example generates CS0283.
// CS0283.cs
struct MyTest
{
}
class MyClass
{
// To resolve the error but retain the "const-ness",
// change const to readonly.
const MyTest test = new MyTest(); // CS0283
public static int Main() {
return 1;
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for