How to Resolve Warning "CS0658: 'Location' is not a recognized attribute location." in C#
The Compiler Warning CS0658 is a syntax warning regarding Attribute Targets. The message reads: "'Target' is not a recognized attribute location. Valid attribute locations for this declaration are 'List'. All attributes in this block will be ignored."
In C#, you can explicitly specify where an attribute applies using a target specifier syntax: [target: Attribute]. For example, [return: SomeAttribute] applies to the return value, not the method itself.
CS0658 occurs when the text before the colon (:) is not a valid C# keyword for an attribute target (often due to a typo), or it is a target that simply does not exist for the specific code element you are decorating. Because the compiler doesn't understand where to put the attribute, it ignores it completely.
Understanding Attribute Targets
When applying attributes, the compiler usually infers the target.
[Obsolete]on a method targets the method.[Serializable]on a class targets the class (type).
Sometimes you need to be specific.
[return: MarshalAs(...)]targets the return value.[field: NonSerialized]targets the backing field of a property.
If you write [retrun: ...] (misspelled), the compiler sees "retrun" as a target identifier, realizes it isn't a reserved keyword, and issues CS0658.
Scenario 1: Typos in Target Names
This is the most common cause. The target specifier must be one of the exact reserved keywords.
Example of warning
A simple spelling error in the target specifier.
using System.Runtime.InteropServices;
public class NativeWrapper
{
// ⛔️ Warning CS0658: 'retrun' is not a recognized attribute location.
// Did you mean 'return'?
[retrun: MarshalAs(UnmanagedType.Bool)]
public bool CheckStatus()
{
return true;
}
}
Solution: Correct the Spelling
Fix the typo to match the C# keyword.
public class NativeWrapper
{
// ✅ Correct: 'return' is the valid target.
[return: MarshalAs(UnmanagedType.Bool)]
public bool CheckStatus()
{
return true;
}
}
Scenario 2: Using Invalid Context Keywords
Sometimes developers assume a keyword works as a target when it does not. For example, trying to target a namespace directly (which is not supported as a target specifier syntax, even though attributes can exist inside namespaces) or using arg instead of param.
Example of warning
// ⛔️ Warning CS0658: 'arg' is not a recognized attribute location.
public void Process([arg: SomeAttribute] int value)
{
}
Solution: Use the Correct Keyword
For parameters, the keyword is param.
// ✅ Correct
public void Process([param: SomeAttribute] int value)
{
}
Valid Attribute Locations Reference
To fix this warning, ensure the text before the colon is one of these exact keywords, and that it makes sense for the item you are decorating:
| Keyword | Description | Valid On |
|---|---|---|
assembly | Attributes for the entire assembly. | Global scope (top of file). |
module | Attributes for the module. | Global scope. |
type | The class, struct, enum, interface, or delegate. | Type declarations. |
method | The method itself. | Methods. |
property | The property itself. | Properties. |
field | The field (or backing field of a property/event). | Fields, Properties, Events. |
param | A method parameter. | Method Parameters. |
return | The return value. | Methods, Delegates. |
event | The event itself. | Events. |
typevar | A generic type parameter (<T>). | Generic definitions. |
Conclusion
CS0658 is the compiler saying: "I don't know what part of the code you are pointing at."
- Check Spelling: "retrun" vs "return", "params" vs "param".
- Check Context: Ensure the target specifier you are using is actually valid for that specific declaration (e.g., you can't use
[return:]on a class). - Remove Ignored Attributes: If the location is invalid, the attribute does nothing. Fix the syntax to ensure your logic (like serialization or marshaling) actually applies.