An attribute target specifies where an attribute can be applied, such as a class, method, property, or assembly.
Click to reveal answer
beginner
How do you specify that an attribute can only be used on methods?
Use [AttributeUsage(AttributeTargets.Method)] above the attribute class definition.
Click to reveal answer
intermediate
What does the AttributeUsage attribute control?
It controls where the custom attribute can be applied and whether it can be applied multiple times or inherited.
Click to reveal answer
intermediate
Can an attribute be applied to multiple targets? How?
Yes, by combining targets using a bitwise OR, for example: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)].
Click to reveal answer
beginner
What happens if you apply an attribute to a target not allowed by its AttributeUsage?
The compiler will show an error because the attribute is used on an invalid target.
Click to reveal answer
Which AttributeTargets value allows an attribute to be applied to properties?
AAttributeTargets.Field
BAttributeTargets.Method
CAttributeTargets.Class
DAttributeTargets.Property
✗ Incorrect
AttributeTargets.Property is used to specify that an attribute can be applied to properties.
How do you allow an attribute to be used multiple times on the same target?
AUse multiple attribute declarations
BSet AllowMultiple = true in AttributeUsage
CSet Inherited = true in AttributeUsage
DUse AttributeTargets.All
✗ Incorrect
AllowMultiple = true in AttributeUsage allows applying the attribute multiple times on the same target.
What is the default target if AttributeUsage is not specified?
AAll targets
BClass only
CMethod only
DProperty only
✗ Incorrect
By default, attributes can be applied to all targets if AttributeUsage is not specified.
Which of these is NOT a valid AttributeTargets value?
AAttributeTargets.Interface
BAttributeTargets.Event
CAttributeTargets.Namespace
DAttributeTargets.Parameter
✗ Incorrect
AttributeTargets.Namespace does not exist; namespaces cannot have attributes.
What does setting Inherited = false in AttributeUsage do?
APrevents derived classes from inheriting the attribute
BAllows multiple attributes on the same target
CRestricts attribute to methods only
DMakes attribute internal
✗ Incorrect
Inherited = false means the attribute is not inherited by derived classes.
Explain how to restrict a custom attribute to be used only on classes and methods.
Think about combining targets with a bitwise OR.
You got /3 concepts.
Describe what happens if you try to apply an attribute to a target not allowed by its AttributeUsage.
Consider compiler checks for attribute placement.
You got /3 concepts.
Practice
(1/5)
1. What does the AttributeUsage attribute control in C#?
easy
A. The visibility of a class
B. The return type of a method
C. Where an attribute can be applied (e.g., class, method, property)
D. The inheritance of a class
Solution
Step 1: Understand the purpose of AttributeUsage
The AttributeUsage attribute 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 C
Quick Check:
AttributeUsage controls attribute targets = C [OK]
Hint: AttributeUsage sets where attributes can be placed [OK]
Common Mistakes:
Confusing AttributeUsage with method or class modifiers
Thinking it controls inheritance or visibility
Assuming it changes method return types
2. Which of the following is the correct syntax to allow an attribute to be applied only to methods?
easy
A. [AttributeUsage(AttributeTargets.Method)]
B. [AttributeUsage(AttributeTargets.Class)]
C. [AttributeUsage(AttributeTargets.Property)]
D. [AttributeUsage(AttributeTargets.Field)]
Solution
Step 1: Identify the target for methods
The AttributeTargets.Method enum 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 A
Quick Check:
Method target uses AttributeTargets.Method = B [OK]
Hint: Use AttributeTargets.Method to target methods only [OK]
Common Mistakes:
Using Class or Property targets instead of Method
Forgetting square brackets around AttributeUsage
Misspelling AttributeTargets
3. What will happen if you apply this attribute to a class?
[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute { }
[MyAttribute]
public class TestClass { }
medium
A. Compilation error because attribute is applied to a class, not a method
B. The attribute is applied successfully to the class
C. Runtime error when the program runs
D. Warning but code compiles
Solution
Step 1: Check the AttributeUsage target
The attribute MyAttribute is restricted to methods only by AttributeTargets.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 A
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class InfoAttribute : Attribute { }
[Info]
[Info]
public class Sample { }
medium
A. No error, multiple attributes allowed
B. Error because AllowMultiple is false but attribute applied twice
C. Error because AttributeTargets is missing Property
D. Error because attribute class must be sealed
Solution
Step 1: Understand AllowMultiple property
The AllowMultiple = false means 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 B
5. You want to create an attribute that can be applied to classes, methods, and properties, and allow multiple uses on the same element. Which is the correct AttributeUsage declaration?
hard
A. [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
B. [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
C. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
D. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
Solution
Step 1: Combine targets for classes, methods, and properties
Use bitwise OR to combine AttributeTargets.Class, AttributeTargets.Method, and AttributeTargets.Property.
Step 2: Set AllowMultiple to true
To allow multiple uses on the same element, set AllowMultiple = true.