Skip to main content

How to Resolve Error "CS0579: Duplicate 'attribute' attribute" in C#

The Compiler Error CS0579 is a declaration error involving Attributes. The message reads: "Duplicate 'AttributeName' attribute".

In C#, attributes are metadata attached to code elements (classes, methods, properties). By default, the C# compiler allows a specific attribute to be applied only once to a single element. If you attempt to apply the same attribute twice (e.g., [Serializable] and [Serializable] again) to the same class, the compiler raises CS0579.

This guide explains how to identify accidental duplications, particularly in partial classes and auto-generated code, and how to allow multiple instances for custom attributes.

Understanding Attribute Usage Rules

When an Attribute is defined, it is decorated with a meta-attribute called [AttributeUsage]. This meta-attribute has a property named AllowMultiple.

  • Default (AllowMultiple = false): The attribute acts like a toggle or a singleton property. It can appear only once per target. (Examples: [Serializable], [Obsolete], [STAThread]).
  • Allowed (AllowMultiple = true): The attribute acts like a list. It can appear multiple times. (Examples: [TestCase] in NUnit, [Route] in ASP.NET).

CS0579 occurs when you stack an attribute that has AllowMultiple = false.

Scenario 1: Redundant Declaration (Typos)

The most obvious cause is simply typing the attribute twice, either on the same line or stacked lines.

Example of error

using System;

namespace MyApp
{
// ⛔️ Error CS0579: Duplicate 'Serializable' attribute.
// This attribute does not support multiple usages.
[Serializable]
[Serializable]
public class UserData
{
public string Name;
}
}

Solution: Remove the Duplicate

Simply delete the extra tag.

using System;

namespace MyApp
{
// ✅ Correct: Only one instance allowed
[Serializable]
public class UserData
{
public string Name;
}
}

Scenario 2: Partial Classes (Hidden Duplicates)

When working with Partial Classes (common in WinForms, WPF, ASP.NET, or Entity Framework), code for a single class is split across multiple files.

If you apply an attribute to the class in File A, and apply the same attribute to the class in File B, the compiler merges them. It sees two attributes on one class, resulting in a collision.

Example of error

File 1 (User.cs):

[Obsolete("Use NewUser class")] // Defined here
public partial class User
{
}

File 2 (User.Generated.cs):

[Obsolete("Use NewUser class")] // ⛔️ Error CS0579: Also defined here!
public partial class User
{
}

Solution: Tag Only One Part

Apply the attribute to only one of the partial files (typically the one you edit manually, not the auto-generated one).

File 1 (User.cs):

[Obsolete("Use NewUser class")] 
public partial class User
{
}

File 2 (User.Generated.cs):

// ✅ Correct: No attribute here. The one in File 1 covers the entire class.
public partial class User
{
}

Scenario 3: AssemblyInfo.cs vs. Auto-Generation

This is the most common cause of this error when migrating from .NET Framework to .NET Core / .NET 5+.

In old projects, assembly attributes (like versioning) lived in a file called Properties\AssemblyInfo.cs. In modern SDK-style projects, these attributes are auto-generated from the .csproj file tags.

If you keep the old AssemblyInfo.cs file in a modern project, the compiler tries to add the attributes from the file and the attributes from the build system, causing duplicates.

Error Message: Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute

Delete the legacy AssemblyInfo.cs file and manage your versioning directly in the .csproj file.

<!-- MyProject.csproj -->
<PropertyGroup>
<Version>1.0.0</Version>
<Company>My Company</Company>
<Description>My App</Description>
</PropertyGroup>

Solution B: Disable Auto-Generation

If you want to keep your manual file, tell the .csproj to stop generating attributes.

<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>

Solution: Allowing Multiple Attributes (Custom Attributes)

If you are the author of the custom attribute causing the error, and you want to allow users to apply it multiple times, you must modify the attribute definition.

The Problematic Attribute

[AttributeUsage(AttributeTargets.Method)] // Defaults to AllowMultiple = false
public class MyTagAttribute : Attribute
{
public string Tag { get; }
public MyTagAttribute(string tag) => Tag = tag;
}

// ⛔️ Error CS0579
[MyTag("A")]
[MyTag("B")]
public void DoWork() { }

The Fix: Update AttributeUsage

Set AllowMultiple = true.

// ✅ Correct: AllowMultiple set to true
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class MyTagAttribute : Attribute
{
public string Tag { get; }
public MyTagAttribute(string tag) => Tag = tag;
}

// ✅ Correct: Now valid
[MyTag("A")]
[MyTag("B")]
public void DoWork() { }

Conclusion

CS0579 ensures configuration consistency.

  1. Check for Typos: Remove redundant lines.
  2. Check Partials: Ensure attributes are only applied in one file of a partial class.
  3. Check AssemblyInfo: If migrating to .NET Core, delete the old AssemblyInfo.cs file or disable generation.
  4. Custom Attributes: Use [AttributeUsage(AllowMultiple = true)] if you intend to support stacking.