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
✗ Incorrect
Custom attribute classes must inherit from System.Attribute to be recognized as attributes.
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
✗ Incorrect
[AttributeUsage] controls the targets and usage rules for the custom 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
✗ Incorrect
You can use either [Info] or [InfoAttribute] when applying the attribute.
Which C# feature allows you to read custom attribute data at runtime?
ASerialization
BDelegates
CLINQ
DReflection
✗ Incorrect
Reflection lets you inspect metadata like custom attributes during program execution.
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]
✗ 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.
Practice
(1/5)
1. What is the main purpose of creating a custom attribute class in C#?
easy
A. To create a new data type for variables
B. To replace the main class in a program
C. To add extra information to code elements like classes or methods
D. To handle exceptions automatically
Solution
Step 1: Understand what attributes do
Attributes add metadata, or extra info, to code elements like classes or methods.
Step 2: Identify the role of custom attributes
Custom attributes let you define your own extra info to attach to code elements.
Final Answer:
To add extra information to code elements like classes or methods -> Option C
Quick Check:
Custom attributes = extra info [OK]
Hint: Attributes add info to code elements, not change logic [OK]
Common Mistakes:
Thinking attributes replace classes
Confusing attributes with data types
Assuming attributes handle errors
2. Which of the following is the correct way to declare a custom attribute class named InfoAttribute?
easy
A. public struct InfoAttribute : Attribute {}
B. public attribute InfoAttribute {}
C. public class InfoAttribute extends Attribute {}
D. public class InfoAttribute : System.Attribute {}
Solution
Step 1: Recall C# syntax for custom attributes
Custom attribute classes must inherit from System.Attribute using a class declaration.
Step 2: Check each option
public class InfoAttribute : System.Attribute {} correctly uses 'public class InfoAttribute : System.Attribute {}'. public attribute InfoAttribute {} uses invalid keyword 'attribute'. public class InfoAttribute extends Attribute {} uses 'extends' which is Java syntax. public struct InfoAttribute : Attribute {} uses 'struct' which is not correct for attributes.
Final Answer:
public class InfoAttribute : System.Attribute {} -> Option D
Quick Check:
Custom attribute = class + : System.Attribute [OK]
Hint: Custom attributes are classes inheriting System.Attribute [OK]
Common Mistakes:
Using 'attribute' keyword instead of 'class'
Using Java syntax 'extends' instead of ':'
Declaring attribute as struct
3. What will be the output of the following code?
using System;
[Info("Version 1.0")]
class Program {}
public class InfoAttribute : Attribute
{
public string Version { get; }
public InfoAttribute(string version) => Version = version;
}
class Test
{
static void Main()
{
var attrs = typeof(Program).GetCustomAttributes(typeof(InfoAttribute), false);
if (attrs.Length > 0)
Console.WriteLine(((InfoAttribute)attrs[0]).Version);
else
Console.WriteLine("No attribute");
}
}
medium
A. Version 1.0
B. No attribute
C. Compilation error
D. Runtime exception
Solution
Step 1: Understand attribute application
The Program class has the Info attribute with version "Version 1.0" applied.
Step 2: Check reflection code
The code uses reflection to get InfoAttribute from Program. Since it exists, it prints the Version property.
Final Answer:
Version 1.0 -> Option A
Quick Check:
Reflection finds attribute version = Version 1.0 [OK]
Hint: Reflection reads attribute properties if applied [OK]
Common Mistakes:
Assuming attribute is not found
Confusing attribute class with target class
Forgetting to cast attribute before accessing property
4. Identify the error in this custom attribute declaration:
public class MyAttribute
{
public string Name { get; set; }
public MyAttribute(string name) { Name = name; }
}
medium
A. Missing inheritance from System.Attribute
B. Property Name should be private
C. Constructor should not have parameters
D. Class should be declared as struct
Solution
Step 1: Check inheritance requirement
Custom attribute classes must inherit from System.Attribute to be recognized as attributes.
Step 2: Analyze given code
The class MyAttribute does not inherit from System.Attribute, so it is not a valid attribute class.
Final Answer:
Missing inheritance from System.Attribute -> Option A
Quick Check:
Attributes must inherit System.Attribute [OK]
Hint: Always inherit System.Attribute for custom attributes [OK]
Common Mistakes:
Forgetting to inherit System.Attribute
Thinking constructor must be parameterless
Using struct instead of class
5. You want to create a custom attribute PriorityAttribute that accepts an integer priority level and can be applied only to methods. Which of the following is the correct way to declare it?
hard
A. [AttributeUsage(AttributeTargets.Class)]
public class PriorityAttribute : Attribute
{
public int Level;
public PriorityAttribute(int level) { Level = level; }
}
B. [AttributeUsage(AttributeTargets.Method)]
public class PriorityAttribute : Attribute
{
public int Level { get; }
public PriorityAttribute(int level) => Level = level;
}
C. public class PriorityAttribute : Attribute
{
public int Level { get; set; }
public PriorityAttribute() {}
}
D. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public struct PriorityAttribute
{
public int Level;
public PriorityAttribute(int level) { Level = level; }
}
Solution
Step 1: Check AttributeUsage target
The attribute should apply only to methods, so AttributeTargets.Method is correct.
Step 2: Verify class declaration and constructor
It must be a class inheriting Attribute, with a constructor accepting an int and a read-only property.
Step 3: Review options
[AttributeUsage(AttributeTargets.Method)]
public class PriorityAttribute : Attribute
{
public int Level { get; }
public PriorityAttribute(int level) => Level = level;
} matches all requirements. [AttributeUsage(AttributeTargets.Class)]
public class PriorityAttribute : Attribute
{
public int Level;
public PriorityAttribute(int level) { Level = level; }
} targets classes, not methods. public class PriorityAttribute : Attribute
{
public int Level { get; set; }
public PriorityAttribute() {}
} lacks constructor parameter and target restriction. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public struct PriorityAttribute
{
public int Level;
public PriorityAttribute(int level) { Level = level; }
} uses struct, which is invalid for attributes.
Final Answer:
[AttributeUsage(AttributeTargets.Method)]
public class PriorityAttribute : Attribute
{
public int Level { get; }
public PriorityAttribute(int level) => Level = level;
} -> Option B
Quick Check:
AttributeUsage + class + constructor with param = [AttributeUsage(AttributeTargets.Method)]
public class PriorityAttribute : Attribute
{
public int Level { get; }
public PriorityAttribute(int level) => Level = level;
} [OK]
Hint: Use AttributeUsage to limit targets and inherit Attribute [OK]
Common Mistakes:
Using struct instead of class
Wrong AttributeTargets in AttributeUsage
Missing constructor parameter for priority
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.