Compiler Error CS0828
Cannot assign 'expression' to anonymous type property.
An anonymous type cannot be initialized with a null value or an unsafe type, or a method group or anonymous function.
To correct this error
- Either add a type declaration to the left side of the assignment, or change the expression on the right side so that it has an acceptable type.
Example
The following code generates CS0828 because a member of an anonymous type cannot be initialized with a null value.
// cs0828.cs
using System;
public class C
{
public static int Main()
{
var a = 1;
var c = new { p1 = null }; // CS0828
return 1;
}
}
See also
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