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:
[Obsolete("Use new method instead")]
public void OldMethod() { }
Click to reveal answer
Which base class must a custom attribute class inherit from in C#?
ASystem.Class
BSystem.Attribute
CSystem.Metadata
DSystem.Object
✗ 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'?
ABoth B and C are correct
B[TestAttribute]
public void Run() { }
C[Test]
public void Run() { }
DYou cannot apply attributes to methods
✗ 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?
A{}
B()
C[]
D<>
✗ 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'?
A[Example: "Hello"]
B[Example('Hello')]
C[Example = "Hello"]
D[Example("Hello")]
✗ Incorrect
Parameters are passed inside parentheses using normal method call syntax.
Which of these is a valid attribute class declaration?
Apublic class MyAttr : System.Attribute { }
Bpublic class MyAttr { }
Cpublic attribute MyAttr { }
Dpublic class MyAttr : AttributeBase { }
✗ 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.
Practice
(1/5)
1. What is the correct way to declare an attribute named Serializable above a class in C#?
easy
A. [Serializable]
public class MyClass { }
B. Serializable
public class MyClass { }
C. (Serializable)
public class MyClass { }
D. {Serializable}
public class MyClass { }
Solution
Step 1: Understand attribute syntax
Attributes in C# are declared using square brackets [] placed above the code element.
Step 2: Apply correct syntax for class attribute
The attribute Serializable must be enclosed in square brackets before the class declaration.
Final Answer:
[Serializable]
public class MyClass { } -> Option A
Quick Check:
Attributes use [] brackets = A [OK]
Hint: Attributes always use square brackets [] above declarations [OK]
Common Mistakes:
Using parentheses () instead of square brackets
Omitting brackets entirely
Using curly braces {} instead of brackets
2. Which of the following is the correct syntax to declare an attribute with a parameter "Test" for Obsolete?
easy
A. [Obsolete("Test")]()
B. [Obsolete: "Test"]
C. [Obsolete = "Test"]
D. [Obsolete("Test")]
Solution
Step 1: Recall attribute parameter syntax
Attribute parameters are passed inside parentheses immediately after the attribute name.
Step 2: Identify correct parameter passing
The string parameter "Test" must be inside parentheses without extra symbols or colons.
Final Answer:
[Obsolete("Test")] -> Option D
Quick Check:
Parameters go inside () after attribute name [OK]
Hint: Use parentheses () for parameters inside square brackets [] [OK]
Common Mistakes:
Using colons or equals signs instead of parentheses
Adding extra parentheses after the attribute
Placing parameters outside brackets
3. What will be the output of the following code?
[Obsolete("Use NewMethod instead")]
public void OldMethod() { Console.WriteLine("Old"); }
public void NewMethod() { Console.WriteLine("New"); }
OldMethod();
medium
A. New
B. Use NewMethod instead
C. Old
D. Compilation error
Solution
Step 1: Understand the Obsolete attribute effect
The Obsolete attribute marks a method as outdated but does not prevent it from running.
Step 2: Analyze method call output
Calling OldMethod() will execute its body and print "Old" to the console.
Final Answer:
Old -> Option C
Quick Check:
Obsolete warns but runs method = Old [OK]
Hint: Obsolete warns but method still runs unless error set [OK]
Common Mistakes:
Thinking Obsolete stops method execution
Expecting warning text as output
Confusing method names in output
4. Identify the error in this attribute declaration:
[Obsolete("Deprecated"]
public void Old() { }
medium
A. Missing closing parenthesis in attribute
B. Missing closing square bracket in attribute
C. Attribute name misspelled
D. Method missing return type
Solution
Step 1: Check attribute brackets and parentheses
The attribute starts with [Obsolete("Deprecated"] but the parentheses are not closed properly.
Step 2: Identify missing closing parenthesis
The string parameter "Deprecated" is opened with a parenthesis but lacks the closing ) before the closing square bracket.
Final Answer:
Missing closing parenthesis in attribute -> Option A
Quick Check:
Parentheses must close before square bracket [OK]
Hint: Check matching () inside [] for attributes [OK]
Common Mistakes:
Forgetting to close parentheses before square bracket
Confusing square brackets with parentheses
Assuming method return type causes attribute error
5. You want to create a custom attribute Info that takes a string parameter description. Which is the correct way to declare it?
hard
A. public attribute Info(string description) { }
B. public class InfoAttribute : Attribute { public InfoAttribute(string description) { } }
C. [Info(string description)] public class MyClass { }
D. public class Info : Attribute { public void Info(string description) { } }
Solution
Step 1: Understand custom attribute class declaration
Custom attributes must be classes derived from Attribute and named with Attribute suffix.
Step 2: Define constructor with parameter
The constructor takes the string parameter description to accept data when used.
Final Answer:
public class InfoAttribute : Attribute { public InfoAttribute(string description) { } } -> Option B
Quick Check:
Custom attribute = class derived from Attribute with constructor [OK]
Hint: Custom attributes are classes ending with Attribute and inherit Attribute [OK]
Common Mistakes:
Using 'attribute' keyword instead of class
Trying to declare attribute parameters in square brackets
Defining method instead of constructor for parameters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.