Attributes add extra information to your code. Attribute targets tell where you can put these attributes, like on a class or a method.
Attribute targets and usage in C Sharp (C#)
[AttributeUsage(AttributeTargets.Target1 | AttributeTargets.Target2, AllowMultiple = true|false)] public class MyAttribute : Attribute { // attribute code here }
AttributeTargets is a set of options like Class, Method, Property, etc.
AllowMultiple controls if you can put the same attribute more than once on the same target.
[AttributeUsage(AttributeTargets.Class)] public class MyClassAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] public class MyMultiUseAttribute : Attribute { }
[AttributeUsage(AttributeTargets.All)] public class MyGeneralAttribute : Attribute { }
This program defines an attribute that can only be used on methods. It marks one method with it and runs that method.
using System; [AttributeUsage(AttributeTargets.Method)] public class RunMeAttribute : Attribute { } public class TestClass { [RunMe] public void MyMethod() { Console.WriteLine("MyMethod is running."); } // This would cause a compile error if uncommented because RunMe is only for methods // [RunMe] // public int MyProperty { get; set; } } class Program { static void Main() { var test = new TestClass(); test.MyMethod(); } }
If you don't specify AttributeUsage, your attribute can be used anywhere and only once per target.
Use AttributeTargets flags combined with | to allow multiple target types.
Setting AllowMultiple to true lets you put the same attribute many times on one target.
Attribute targets control where an attribute can be applied.
Use AttributeUsage to set targets and if multiple uses are allowed.
This helps keep your code clear and prevents wrong attribute usage.
Practice
AttributeUsage attribute control in C#?Solution
Step 1: Understand the purpose of AttributeUsage
TheAttributeUsageattribute specifies the valid program elements (targets) where a custom attribute can be applied, such as classes, methods, or properties.Step 2: Eliminate unrelated options
Options about return types, visibility, or inheritance are unrelated to attribute targets.Final Answer:
Where an attribute can be applied (e.g., class, method, property) -> Option CQuick Check:
AttributeUsage controls attribute targets = C [OK]
- Confusing AttributeUsage with method or class modifiers
- Thinking it controls inheritance or visibility
- Assuming it changes method return types
Solution
Step 1: Identify the target for methods
TheAttributeTargets.Methodenum value specifies that the attribute can only be applied to methods.Step 2: Match the syntax
The correct syntax uses[AttributeUsage(AttributeTargets.Method)]to restrict usage to methods.Final Answer:
[AttributeUsage(AttributeTargets.Method)] -> Option AQuick Check:
Method target uses AttributeTargets.Method = B [OK]
- Using Class or Property targets instead of Method
- Forgetting square brackets around AttributeUsage
- Misspelling AttributeTargets
[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute { }
[MyAttribute]
public class TestClass { }Solution
Step 1: Check the AttributeUsage target
The attributeMyAttributeis restricted to methods only byAttributeTargets.Method.Step 2: Analyze the application
Applying[MyAttribute]to a class violates the target restriction, causing a compilation error.Final Answer:
Compilation error because attribute is applied to a class, not a method -> Option AQuick Check:
Wrong target usage causes compile error = D [OK]
- Assuming attribute applies anyway without error
- Thinking error happens at runtime
- Ignoring AttributeUsage restrictions
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class InfoAttribute : Attribute { }
[Info]
[Info]
public class Sample { }Solution
Step 1: Understand AllowMultiple property
TheAllowMultiple = falsemeans the attribute cannot be applied more than once on the same element.Step 2: Check attribute usage on Sample class
The[Info]attribute is applied twice on the same class, violating the AllowMultiple restriction.Final Answer:
Error because AllowMultiple is false but attribute applied twice -> Option BQuick Check:
AllowMultiple false + multiple uses = error [OK]
- Ignoring AllowMultiple setting
- Thinking multiple attributes are always allowed
- Confusing AttributeTargets with AllowMultiple
AttributeUsage declaration?Solution
Step 1: Combine targets for classes, methods, and properties
Use bitwise OR to combineAttributeTargets.Class,AttributeTargets.Method, andAttributeTargets.Property.Step 2: Set AllowMultiple to true
To allow multiple uses on the same element, setAllowMultiple = true.Final Answer:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] -> Option DQuick Check:
Combine targets + AllowMultiple true = A [OK]
- Using AllowMultiple false when multiple uses needed
- Missing some targets in the bitwise OR
- Using AttributeTargets.All when not needed
Ready to prove it?
Pass the test and this pattern moves unknown
