How to Resolve Error "CS0833: An anonymous type cannot have multiple properties with the same name" in C#
The Compiler Error CS0833 is a Naming Collision error specific to Anonymous Types. The message reads: "An anonymous type cannot have multiple properties with the same name".
In C#, creating an anonymous type (e.g., new { Name = "Alice", Age = 25 }) instructs the compiler to generate a class behind the scenes. Just like a standard class cannot have two properties named ID, an anonymous type cannot define the same property name twice.
This error most frequently occurs during LINQ projections when the compiler automatically infers property names from the source data, resulting in accidental duplicates.
Understanding Property Inference
When you declare an anonymous type, you can name the properties explicitly:
new { MyProp = value }
Or you can let the compiler infer the name from the variable or property being assigned:
new { user.Name }` is compiled as `new { Name = user.Name }
CS0833 happens when this inference or explicit naming results in { Name = ..., Name = ... }.
Scenario 1: Explicit Duplicate Assignments
This is the simplest form of the error. You explicitly typed the same identifier twice inside the initialization braces.
Example of error
public void CreateData()
{
// ⛔️ Error CS0833: An anonymous type cannot have multiple properties with the same name.
// You declared 'ID' twice.
var data = new { ID = 1, Name = "Test", ID = 2 };
}
Solution: Remove or Rename
Ensure every property in the anonymous object has a unique name.
public void CreateData()
{
// ✅ Correct: Unique property names
var data = new { ID = 1, Name = "Test", SecondaryID = 2 };
}
Scenario 2: Duplicate Inferred Names (Common LINQ Error)
This scenario is very common when joining tables or objects in LINQ. If you select data from two different sources that happen to share a property name (e.g., User.Name and Company.Name), relying on implicit naming will cause a collision.
Example of error
The compiler tries to name both properties Name, which is invalid.
public class User { public string Name { get; set; } }
public class Company { public string Name { get; set; } }
public class Program
{
static void Main()
{
var user = new User { Name = "Alice" };
var company = new Company { Name = "TechCorp" };
// ⛔️ Error CS0833: The compiler infers: new { Name = ..., Name = ... }
// It cannot create a class with two 'Name' properties.
var result = new { user.Name, company.Name };
}
}
Solution: Use Explicit Aliases
You must explicitly name at least one (preferably both) of the properties to resolve the ambiguity.
public class Program
{
static void Main()
{
var user = new User { Name = "Alice" };
var company = new Company { Name = "TechCorp" };
// ✅ Correct: We provide explicit names (Aliases) for the properties.
var result = new
{
UserName = user.Name,
CompanyName = company.Name
};
Console.WriteLine($"User: {result.UserName}, Co: {result.CompanyName}");
}
}
LINQ Select Example:
The same fix applies to LINQ queries:
list.Select(x => new { x.User.Name, x.Company.Name }) fails.
list.Select(x => new { UserName = x.User.Name, CompanyName = x.Company.Name }) succeeds.
Conclusion
CS0833 is a request for uniqueness.
- Check your
new { ... }block: Are you assigning variables? - Check Inferred Names: If you write
new { a.Id, b.Id }, the compiler seesIdtwice. - Alias Them: Change it to
new { UserID = a.Id, OrderID = b.Id }to ensure every property has a distinct identity.