How to Resolve Error "CS0643: Duplicate named attribute argument" in C#
The Compiler Error CS0643 is a syntax error related to Attribute usage. The message reads: "Duplicate named attribute argument".
In C#, when you apply an attribute to a class, method, or property, you can initialize its public properties or fields using Named Arguments (syntax: Property = Value). This error occurs when you attempt to assign a value to the same property name more than once within a single attribute declaration. The compiler flags this because it is ambiguous and redundant; it cannot decide which value should prevail.
This guide explains how named arguments work and how to resolve this duplication.
Understanding Named Arguments in Attributes
Attributes typically have two types of parameters:
- Positional Arguments: Passed to the attribute's constructor.
- Named Arguments: Optional settings that map directly to public properties or fields of the attribute class.
[MyAttribute("ConstructorValue", SettingA = 1, SettingB = 2)]
Since SettingA refers to a specific storage slot in the attribute object, trying to set SettingA twice in the same list is logically impossible, just like writing obj.SettingA = 1; obj.SettingA = 2; in a single initialization statement.
Scenario: Assigning the Same Property Twice
This error often occurs due to copy-paste mistakes or a misunderstanding of how attributes handle lists of data.
Example of error: in this example, the developer tries to give the Description property two different values in the same line.
using System;
[AttributeUsage(AttributeTargets.Class)]
public class MetaDataAttribute : Attribute
{
public string Description { get; set; }
public string Author { get; set; }
}
// ⛔️ Error CS0643: Duplicate named attribute argument 'Description'
// The compiler sees 'Description' being set to "Primary" AND "Secondary".
[MetaData(Description = "Primary", Author = "Alice", Description = "Secondary")]
public class MyClass
{
}
Solution 1: Remove the Duplicate (Correction)
If this was a typo or a copy-paste error, simply remove the redundant assignment. Decide which value is the correct one.
// ✅ Correct: Each property is assigned exactly once.
[MetaData(Description = "Secondary", Author = "Alice")]
public class MyClass
{
}
Solution 2: Stack Multiple Attributes (Intent Check)
Sometimes, developers write duplicates because they actually want to apply the attribute twice with different values (e.g., mapping a method to two different URL routes).
You cannot achieve this by setting the property twice in one line. Instead, you must apply the attribute declaration twice (provided the attribute is marked with AllowMultiple = true).
Solution for Multiple Values:
using System;
// Ensure the attribute allows multiple usage
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class TagAttribute : Attribute
{
public string Name { get; set; }
}
// ✅ Correct: Stack the attributes to apply multiple tags.
// Do NOT write: [Tag(Name = "V1", Name = "Alpha")]
[Tag(Name = "V1")]
[Tag(Name = "Alpha")]
public class VersionedClass
{
}
If the attribute does not support AllowMultiple = true, you cannot stack them. In that case, you must choose a single value or redesign the attribute to accept an array/list as a constructor argument.
Conclusion
CS0643 is a redundancy check.
- Check the assignment: Look inside the
[Attribute(...)]parentheses. - Find the duplicate: Did you type
Name = "A"and laterName = "B"? - Decide:
- If you meant to overwrite, keep only the final value.
- If you meant to add another entry, use a second attribute line (e.g.,
[Attr...] [Attr...]), assuming the attribute supports it.