Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Attribute targets and usage in C Sharp (C#) - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify that the attribute can only be applied to classes.

C Sharp (C#)
[AttributeUsage([1])]
public class MyCustomAttribute : Attribute
{
}
Drag options to blanks, or click blank then click option'
AAttributeTargets.Class
BAttributeTargets.Method
CAttributeTargets.Property
DAttributeTargets.Field
Attempts:
3 left
💡 Hint
Common Mistakes
Using AttributeTargets.Method or other targets instead of AttributeTargets.Class.
2fill in blank
medium

Complete the code to allow the attribute to be applied multiple times on the same element.

C Sharp (C#)
[AttributeUsage(AttributeTargets.Method, [1] = true)]
public class RepeatableAttribute : Attribute
{
}
Drag options to blanks, or click blank then click option'
AInherited
BAllowMultiple
CValidOn
DMultipleAllowed
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Inherited' or incorrect property names like 'ValidOn' or 'MultipleAllowed'.
3fill in blank
hard

Fix the error in the attribute usage by completing the code with the correct target.

C Sharp (C#)
[AttributeUsage([1])]
public class SampleAttribute : Attribute
{
}
Drag options to blanks, or click blank then click option'
AAttributeTargets.Namespace
BAttributeTargets.ReturnValue
CAttributeTargets.Parameter
DAttributeTargets.Class | AttributeTargets.Method
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid targets like Namespace or only one target when multiple are needed.
4fill in blank
hard

Fill both blanks to create an attribute that can be inherited and applied to properties.

C Sharp (C#)
[AttributeUsage([1], [2] = true)]
public class InheritablePropertyAttribute : Attribute
{
}
Drag options to blanks, or click blank then click option'
AAttributeTargets.Property
BAllowMultiple
CInherited
DAttributeTargets.Field
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing AllowMultiple with Inherited or using wrong targets like Field.
5fill in blank
hard

Fill all three blanks to define an attribute that targets methods, allows multiple usage, and is not inherited.

C Sharp (C#)
[AttributeUsage([1], [2] = true, [3] = false)]
public class CustomMethodAttribute : Attribute
{
}
Drag options to blanks, or click blank then click option'
AAttributeTargets.Method
BAllowMultiple
CInherited
DAttributeTargets.Class
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up AllowMultiple and Inherited or using wrong targets.

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

  1. 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.
  2. Step 2: Eliminate unrelated options

    Options about return types, visibility, or inheritance are unrelated to attribute targets.
  3. Final Answer:

    Where an attribute can be applied (e.g., class, method, property) -> Option C
  4. 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

  1. Step 1: Identify the target for methods

    The AttributeTargets.Method enum value specifies that the attribute can only be applied to methods.
  2. Step 2: Match the syntax

    The correct syntax uses [AttributeUsage(AttributeTargets.Method)] to restrict usage to methods.
  3. Final Answer:

    [AttributeUsage(AttributeTargets.Method)] -> Option A
  4. 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

  1. Step 1: Check the AttributeUsage target

    The attribute MyAttribute is restricted to methods only by AttributeTargets.Method.
  2. Step 2: Analyze the application

    Applying [MyAttribute] to a class violates the target restriction, causing a compilation error.
  3. Final Answer:

    Compilation error because attribute is applied to a class, not a method -> Option A
  4. Quick Check:

    Wrong target usage causes compile error = D [OK]
Hint: AttributeUsage restricts targets; wrong target causes compile error [OK]
Common Mistakes:
  • Assuming attribute applies anyway without error
  • Thinking error happens at runtime
  • Ignoring AttributeUsage restrictions
4. Identify the error in this code snippet:
[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

  1. Step 1: Understand AllowMultiple property

    The AllowMultiple = false means the attribute cannot be applied more than once on the same element.
  2. Step 2: Check attribute usage on Sample class

    The [Info] attribute is applied twice on the same class, violating the AllowMultiple restriction.
  3. Final Answer:

    Error because AllowMultiple is false but attribute applied twice -> Option B
  4. Quick Check:

    AllowMultiple false + multiple uses = error [OK]
Hint: AllowMultiple false forbids multiple attribute uses [OK]
Common Mistakes:
  • Ignoring AllowMultiple setting
  • Thinking multiple attributes are always allowed
  • Confusing AttributeTargets with AllowMultiple
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

  1. Step 1: Combine targets for classes, methods, and properties

    Use bitwise OR to combine AttributeTargets.Class, AttributeTargets.Method, and AttributeTargets.Property.
  2. Step 2: Set AllowMultiple to true

    To allow multiple uses on the same element, set AllowMultiple = true.
  3. Final Answer:

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] -> Option D
  4. Quick Check:

    Combine targets + AllowMultiple true = A [OK]
Hint: Use bitwise OR for multiple targets and AllowMultiple true [OK]
Common Mistakes:
  • Using AllowMultiple false when multiple uses needed
  • Missing some targets in the bitwise OR
  • Using AttributeTargets.All when not needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard