Skip to main content

How to Resolve Error "CS0828: Cannot assign 'expression' to anonymous type property" in C#

The Compiler Error CS0828 is a Type Inference error. The message reads: "Cannot assign 'expression' to anonymous type property".

In C#, Anonymous Types (new { Prop = Value }) rely entirely on the compiler to figure out the data types of their properties based on the values you assign. If you assign an expression that does not have a definitive type—such as null, a lambda expression, or a method group—the compiler cannot generate the underlying class definition, resulting in CS0828.

This guide explains which expressions lack implicit types and how to cast them to resolve the error.

Understanding Anonymous Type Inference

When you write:

var obj = new { Age = 25, Name = "Alice" };

The compiler generates a class behind the scenes roughly like: class __Anonymous<T1, T2>. It infers that Age is int and Name is string.

However, if you write:

var obj = new { Data = null };

The compiler asks: "What is Data? Is it a string? A nullable int? An object?" Since null fits into any reference type, the compiler refuses to guess.

Scenario 1: Assigning null

This is the most common cause. You are initializing an object with a default empty value, but null alone carries no type information.

Example of error

public class Program
{
static void Main()
{
// ⛔️ Error CS0828: Cannot assign '<null>' to anonymous type property.
// The compiler cannot determine the type of 'MissingName'.
var user = new { Id = 1, MissingName = null };
}
}

Solution: Explicit Cast

You must cast null to a specific type so the compiler knows what property type to generate.

public class Program
{
static void Main()
{
// ✅ Correct: We explicitly tell the compiler 'MissingName' is a string.
var user = new { Id = 1, MissingName = (string)null };

// Alternatively, for nullable value types:
// var config = new { Limit = (int?)null };
}
}

Scenario 2: Assigning Lambda Expressions

Lambda expressions (e.g., x => x + 1) do not have a type in themselves. They can be converted to either a Delegate (executable code) or an Expression Tree (data representing code). Because of this duality, the compiler cannot infer which one you want inside an anonymous type.

Example of error

public class Program
{
static void Main()
{
// ⛔️ Error CS0828: Cannot assign lambda to anonymous type property.
// Is 'Square' a Func<int, int> or Expression<Func<int, int>>?
var math = new { Square = (int x) => x * x };
}
}

Solution: Cast to a Delegate Type

Cast the lambda to a specific delegate type (like Func or Action).

using System;

public class Program
{
static void Main()
{
// ✅ Correct: Cast the lambda to Func<int, int>
var math = new { Square = (Func<int, int>)((x) => x * x) };

Console.WriteLine(math.Square(5)); // Output: 25
}
}

Scenario 3: Assigning Method Groups

A "Method Group" is the name of a method without parentheses (e.g., Console.WriteLine). Like lambdas, method groups can be converted to various compatible delegate types. Without a target type, the assignment is ambiguous.

Example of error

public class Program
{
static void Main()
{
// ⛔️ Error CS0828: Cannot assign method group to anonymous type property.
var logger = new { LogAction = Console.WriteLine };
}
}

Solution: Cast to Action or Func

Specify the delegate signature required.

using System;

public class Program
{
static void Main()
{
// ✅ Correct: Explicitly cast to an Action<string>
var logger = new { LogAction = (Action<string>)Console.WriteLine };

logger.LogAction("Hello World");
}
}

Conclusion

CS0828 means the compiler needs more information to build your anonymous class.

  1. Check for Null: If assigning null, cast it: (string)null.
  2. Check for Functions: If assigning lambdas or methods, cast them to a delegate: (Func<int>)MyMethod.
  3. Check for Unsafe Pointers: Anonymous types cannot contain unsafe pointers; this is strictly forbidden and requires a defined struct instead.