Skip to main content

How to Resolve Error "CS0746: Invalid anonymous type member declarator" in C#

The Compiler Error CS0746 is a syntax error regarding Anonymous Types. The message reads: "Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access."

In C#, when you create an anonymous type using new { ... }, the compiler generates a class behind the scenes. Every property in this class must have a Name and a Value.

  • Explicit Naming: new { Age = 10 } (Name is "Age").
  • Name Inference (Projection): new { user.Name } (Name is inferred as "Name").

The error occurs when you provide a value (like a calculation, a method call, or a literal) but fail to provide a name, and the compiler cannot infer one automatically.

This guide explains how to ensure every member of your anonymous type is correctly named.

Understanding Name Inference

When declaring new { A, B }, the compiler tries to be helpful:

  • If A is a variable int count, the property becomes Count.
  • If A is a property access obj.Name, the property becomes Name.

However, if A is x + y or GetID(), the compiler cannot determine a valid property name for the generated class. You cannot name a property "x+y" or "GetID()". In these cases, you must explicitly assign a name.

Scenario 1: Expressions and Calculations

This is the most common cause. You perform math or string concatenation inside the anonymous type declaration but forget to assign the result to a named property.

Example of error

public void ProcessMath(int width, int height)
{
// ⛔️ Error CS0746: Invalid anonymous type member declarator.
// The compiler sees 'width * height'. It cannot infer a name for this calculation.
var shape = new { width, height, width * height };
}

Solution: Explicit Assignment

Give the calculated value a name using Name = Value syntax.

public void ProcessMath(int width, int height)
{
// ✅ Correct: We explicitly name the third property 'Area'.
var shape = new { width, height, Area = width * height };

System.Console.WriteLine($"Area: {shape.Area}");
}

Scenario 2: Method Calls

While property access (e.g., p.Name) allows name inference, method calls (e.g., p.GetName()) do not. The compiler does not strip the () to create a name automatically.

Example of error

public class User
{
public string GetRole() => "Admin";
}

public class Program
{
static void Main()
{
var u = new User();

// ⛔️ Error CS0746: Method calls cannot infer a property name.
var data = new { u.GetRole() };
}
}

Solution: Assign a Name

Assign the result of the method to a property name.

public class Program
{
static void Main()
{
var u = new User();

// ✅ Correct: The property is named 'Role'.
var data = new { Role = u.GetRole() };
}
}

Scenario 3: Literals and Arrays

You cannot simply list raw values (literals) or array index accesses inside an anonymous type without names.

Example of error

public void CreateRecord()
{
string[] tags = { "C#", "Error" };

// ⛔️ Error CS0746: '100' is a literal. 'tags[0]' is an index access.
// Neither has a simple identifier name.
var record = new { 100, tags[0] };
}

Solution: Assign Names

Define what these values represent.

public void CreateRecord()
{
string[] tags = { "C#", "Error" };

// ✅ Correct: Explicit names provided.
var record = new { Id = 100, MainTag = tags[0] };
}

Conclusion

CS0746 is the compiler asking: "What should I call this property?"

  1. Check the Syntax: Are you using new { value }?
  2. Determine the Source:
    • If it is a Variable or Property (e.g., x, obj.Prop), inference works.
    • If it is a Calculation, Method, or Literal, inference fails.
  3. The Fix: Use new { Name = value } to manually name the property.