How to Resolve Warning "CS0657: 'attribute' is not a valid attribute location for this declaration" in C#
The Compiler Warning CS0657 is a usage warning regarding Attributes. The message reads: " 'AttributeName' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'Location'. All attributes in this block will be ignored."
In C#, attributes can be applied to various code elements (like classes, methods, parameters, or return values). However, some attributes are restricted to specific targets (e.g., an attribute might only be valid on a method, but you tried to put it on a class). Alternatively, you might have placed the attribute in a spot where the compiler defaults to a different target (like putting an attribute on a method that was intended for the return value) without explicitly specifying the target.
This guide explains how attribute targets work and how to fix this warning.
Understanding Attribute Targets
When you place an attribute like [MyAttribute] above a method, the compiler guesses what you want to tag based on context.
- Above a class $\rightarrow$ Target:
type - Above a method $\rightarrow$ Target:
method - Inside parameters $\rightarrow$ Target:
parameter
However, some attributes are designed for the Return Value of a method, or the Assembly itself. To target these "hidden" elements, you must use a Target Specifier (syntax: [target: Attribute]).
If you use a target specifier that doesn't make sense for the declaration (e.g., trying to target [return: ...] on a class definition), CS0657 triggers.
Scenario 1: Misplaced Target Specifiers (return vs. method)
This warning often appears when developers copy-paste attributes like [return: MarshalAs(...)] but accidentally paste them onto a Class or Field where a "return value" concept does not exist.
Example of warning
Attempting to apply a return target attribute to a class.
using System.Runtime.InteropServices;
// ⛔️ Warning CS0657: 'return' is not a valid attribute location for this declaration.
// Valid locations for a class declaration are 'type'.
[return: MarshalAs(UnmanagedType.U4)]
public class MyData
{
}
Solution: Remove or Correct the Target
If the attribute is meant for the class, remove return:. If the attribute was meant for a method inside the class, move it.
Option A: Fix the Target
// ✅ Correct: No specifier needed (defaults to 'type') or use [type: ...]
[StructLayout(LayoutKind.Sequential)]
public class MyData
{
}
Option B: Move to Method
public class MyData
{
// ✅ Correct: 'return' is valid on a method declaration
[return: MarshalAs(UnmanagedType.U4)]
public int GetData()
{
return 42;
}
}
Scenario 2: Attributes on Invalid Elements
Some attributes are strictly defined to work only on specific elements (defined by their [AttributeUsage]). While this usually causes a different error (CS0592), using an explicit target specifier that contradicts the declaration will specifically trigger CS0657.
Example of warning
Trying to target a param on a property definition (properties don't have parameters in the standard sense, indexers do).
public class User
{
// ⛔️ Warning CS0657: 'param' is not a valid attribute location for this declaration.
// A simple property has no parameters to tag.
[param: SomeAttribute]
public string Name { get; set; }
}
Solution
Remove the invalid target specifier.
public class User
{
// ✅ Correct: Apply to the property itself
[SomeAttribute]
public string Name { get; set; }
}
Conclusion
CS0657 means you are pointing at something that isn't there.
- Check the Prefix: Are you using
[return: ...],[assembly: ...], or[param: ...]? - Check the Context: specific prefix is allowed on the code element you are decorating?
[return:]$\rightarrow$ Only on Methods/Delegates.[assembly:]$\rightarrow$ Anywhere (but applies globally), usually top of file.[field:]$\rightarrow$ On Properties (to tag the backing field) or Events.
- Fix: Remove the prefix or move the attribute to the correct line of code.