Recall & Review
beginner
What is an attribute in C#?
An attribute in C# is a way to add metadata to your code, like extra information about classes, methods, or properties that can be used by the compiler or runtime.
Click to reveal answer
beginner
How do you declare an attribute in C#?
You declare an attribute by creating a class that inherits from System.Attribute and usually ends with the word 'Attribute'.Click to reveal answer
beginner
What is the syntax to apply an attribute to a class or method?You place the attribute name in square brackets above the class or method. For example: <br><pre>[Serializable]<br>public class MyClass { }</pre>Click to reveal answer
intermediate
Can attribute names be shortened when applied? How?
Yes. When applying an attribute, you can omit the 'Attribute' suffix. For example, [Serializable] is short for [SerializableAttribute].
Click to reveal answer
intermediate
How do you pass parameters to an attribute?
You pass parameters inside parentheses after the attribute name, like a method call. For example: <br>
[Obsolete("Use new method instead")]
public void OldMethod() { }Click to reveal answer
Which base class must a custom attribute class inherit from in C#?
✗ Incorrect
All custom attributes must inherit from System.Attribute to be recognized as attributes.
How do you apply an attribute named 'TestAttribute' to a method named 'Run'?
✗ Incorrect
You can use either [Test] or [TestAttribute] because the 'Attribute' suffix is optional when applying.
What symbol is used to declare an attribute above a class or method?
✗ Incorrect
Attributes are declared using square brackets [] above the target code element.
How do you pass a string parameter 'Hello' to an attribute named 'Example'?
✗ Incorrect
Parameters are passed inside parentheses using normal method call syntax.
Which of these is a valid attribute class declaration?
✗ Incorrect
Custom attributes must inherit from System.Attribute to be valid.
Explain how to declare and apply a custom attribute in C#.
Think about how you add extra info to classes or methods.
You got /4 concepts.
Describe how to pass parameters to an attribute and why it might be useful.
Consider how you give extra details to the attribute.
You got /4 concepts.