0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 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.