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

Custom attribute classes in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
ASystem.Metadata
BSystem.Attribute
CSystem.CustomAttribute
DSystem.Object
What does the [AttributeUsage] attribute specify?
AThe visibility of the attribute class
BThe return type of the attribute
CWhere and how the custom attribute can be applied
DThe namespace of the attribute
How do you apply a custom attribute named InfoAttribute to a class?
ABoth B and C
B[InfoAttribute]
C[Info]
DYou cannot apply it to classes
Which C# feature allows you to read custom attribute data at runtime?
ASerialization
BDelegates
CLINQ
DReflection
Can a custom attribute have parameters in its constructor?
AYes, to pass data when applying the attribute
BNo, attributes cannot have constructors
COnly static parameters
DOnly if marked with [Serializable]
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.