Recall & Review
beginner
What is a custom attribute class in C#?A custom attribute class is a special class that you create to add extra information (metadata) to your code elements like classes, methods, or properties. It helps you store data that can be read later using reflection.Click to reveal answer
beginner
How do you define a custom attribute class in C#?You create a class that inherits from <code>System.Attribute</code>. This class can have properties or fields to hold the extra information you want to attach.Click to reveal answer
intermediate
What is the purpose of the
[AttributeUsage] attribute when creating custom attributes?[AttributeUsage] controls where your custom attribute can be applied (like classes, methods, properties) and whether it can be used multiple times on the same element.Click to reveal answer
beginner
How do you apply a custom attribute to a method in C#?
You place the attribute above the method using square brackets, like <code>[MyCustomAttribute]</code>. If the attribute class name ends with 'Attribute', you can omit that part when applying it.Click to reveal answer
intermediate
How can you read the values of a custom attribute at runtime?
You use reflection to get the attribute from the code element and then access its properties or fields to read the stored information.
Click to reveal answer
Which class must a custom attribute class inherit from in C#?
✗ Incorrect
Custom attribute classes must inherit from System.Attribute to be recognized as attributes.
What does the
[AttributeUsage] attribute specify?✗ Incorrect
[AttributeUsage] controls the targets and usage rules for the custom attribute.How do you apply a custom attribute named
InfoAttribute to a class?✗ Incorrect
You can use either [Info] or [InfoAttribute] when applying the attribute.
Which C# feature allows you to read custom attribute data at runtime?
✗ Incorrect
Reflection lets you inspect metadata like custom attributes during program execution.
Can a custom attribute have parameters in its constructor?
✗ Incorrect
Custom attributes can have constructors with parameters to accept data when used.
Explain how to create and apply a custom attribute class in C#.
Think about making a class that holds extra info and then tagging your code with it.
You got /4 concepts.
Describe how to retrieve and use custom attribute information at runtime.
Imagine looking up notes you attached to your code while the program runs.
You got /3 concepts.