How to Resolve Error "CS0520: Predefined type 'type' is declared incorrectly" in C#
The Compiler Error CS0520 is a foundational definition error. The message reads: "Predefined type 'System.Object' (or other types) is declared incorrectly".
In scenarios where you are building a custom core library (using /nostdlib), you are responsible for defining the basic system types like System.Object, System.String, and System.Int32. However, the Common Language Runtime (CLR) has strict expectations about how these types must be structured. If you define them, but fail to match the required internal layout (e.g., System.Object must have no base class, System.String must be a sealed class, etc.), the compiler raises CS0520.
This guide explains the specific requirements for fundamental types in a custom runtime environment.
Understanding Predefined Type Requirements
Normally, mscorlib.dll or System.Runtime.dll provides these definitions. When you take over this responsibility (by enabling <NoStdLib>true</NoStdLib>), you must define them exactly as the runtime expects.
Common constraints include:
System.Object: Must be the root of the hierarchy (no base class).System.ValueType: Must inherit fromSystem.Object.System.String: Must besealedand effectively wrap a character buffer.System.Array: Must inherit fromSystem.Objectand provide specific internal interfaces.
Scenario 1: System.Object Inheritance
This is the most fundamental error. System.Object is the root of everything. It cannot inherit from anything else.
Example of error
Accidentally inheriting from another class, or creating a circular dependency.
namespace System
{
// ⛔️ Error CS0520: Predefined type 'System.Object' is declared incorrectly.
// System.Object cannot have a base class.
public class Object : SomeOtherClass
{
}
}
Solution: No Base Class
Ensure System.Object stands alone.
namespace System
{
// ✅ Correct: The root of the type hierarchy.
public class Object
{
// Essential method for the runtime
public virtual string ToString() => GetType().ToString();
}
}
Scenario 2: System.String Modifiers
The string type is heavily optimized by the runtime. It must be marked sealed because the runtime does not support inheriting from string.
Example of error
Defining String as an open class.
namespace System
{
// ⛔️ Error CS0520: Predefined type 'System.String' is declared incorrectly.
// The runtime requires String to be sealed.
public class String
{
}
}
Solution: Seal the Class
Add the sealed modifier.
namespace System
{
// ✅ Correct: Matches CLR expectations.
public sealed class String
{
public readonly int Length;
// ... implementation details ...
}
}
Conclusion
CS0520 is an error for compiler/runtime architects.
- Check Project Config: If you see this in a standard web/console app, you likely have
<NoStdLib>enabled by accident. Remove it. - Verify Definitions: If you are writing a custom mscorlib, verify your type definitions against the official source (source.dot.net) to ensure you match the CLR's expected structure exactly.