Difficulty: Easy
Correct Answer: [assembly: Tested('Sachin', testgrade.Good)] and [Tested('Virat', testgrade.Excellent)] class Customer { /.../ } are both valid usages
Explanation:
Introduction / Context:
Custom attributes in C# are user-defined classes derived from System.Attribute. Once created, they can be applied to a variety of targets, including assemblies and classes, provided their AttributeUsage allows those targets.
Given Data / Assumptions:
Concept / Approach:
Assembly-level attributes use the special target specifier inside the brackets: [assembly: …]. Class-level attributes are placed directly above the class declaration. Both are standard and valid, assuming AttributeUsage supports those targets.
Step-by-Step Solution:
Verification / Alternative check:
Create a minimal TestedAttribute : Attribute class with [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)], apply at assembly and class scope, and compile successfully.
Why Other Options Are Wrong:
They assert impossibilities (e.g., cannot apply to assemblies or classes) that contradict the attribute system.
Common Pitfalls:
Forgetting the assembly target specifier; assuming attributes default to all targets without AttributeUsage configuration.
Final Answer:
[assembly: Tested('Sachin', testgrade.Good)] and [Tested('Virat', testgrade.Excellent)] class Customer { /.../ } are both valid usages
Discussion & Comments