Custom attribute classes in C Sharp (C#) - Time & Space Complexity
When we use custom attribute classes in C#, it's important to understand how the program's work grows as we apply these attributes to many parts of the code.
We want to know how the time to check or use these attributes changes when the number of classes or methods with attributes increases.
Analyze the time complexity of the following code snippet.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class MyCustomAttribute : Attribute
{
public string Info { get; }
public MyCustomAttribute(string info) => Info = info;
}
public class Example
{
[MyCustomAttribute("Test")]
public void Method() { }
}
// Later, reflection is used to find attributes:
var methods = typeof(Example).GetMethods();
foreach (var method in methods)
{
var attrs = method.GetCustomAttributes(typeof(MyCustomAttribute), false);
// process attrs
}
This code defines a custom attribute and then uses reflection to find all methods with that attribute.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all methods of a class to check for custom attributes.
- How many times: Once for each method in the class.
As the number of methods increases, the program checks each method one by one for the attribute.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 attribute checks |
| 100 | About 100 attribute checks |
| 1000 | About 1000 attribute checks |
Pattern observation: The work grows directly with the number of methods; doubling methods doubles the checks.
Time Complexity: O(n)
This means the time to find attributes grows in a straight line with the number of methods checked.
[X] Wrong: "Checking attributes is instant and does not depend on how many methods exist."
[OK] Correct: Each method must be checked one by one, so more methods mean more work.
Understanding how attribute checks scale helps you write efficient code and reason about reflection performance in real projects.
What if we cached the attributes after the first check? How would the time complexity change when accessing attributes multiple times?
Practice
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 CQuick Check:
Custom attributes = extra info [OK]
- Thinking attributes replace classes
- Confusing attributes with data types
- Assuming attributes handle errors
InfoAttribute?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 DQuick Check:
Custom attribute = class + : System.Attribute [OK]
- Using 'attribute' keyword instead of 'class'
- Using Java syntax 'extends' instead of ':'
- Declaring attribute as struct
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");
}
}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 AQuick Check:
Reflection finds attribute version = Version 1.0 [OK]
- Assuming attribute is not found
- Confusing attribute class with target class
- Forgetting to cast attribute before accessing property
public class MyAttribute
{
public string Name { get; set; }
public MyAttribute(string name) { Name = name; }
}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 AQuick Check:
Attributes must inherit System.Attribute [OK]
- Forgetting to inherit System.Attribute
- Thinking constructor must be parameterless
- Using struct instead of class
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?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 BQuick Check:
AttributeUsage + class + constructor with param = [AttributeUsage(AttributeTargets.Method)] public class PriorityAttribute : Attribute { public int Level { get; } public PriorityAttribute(int level) => Level = level; } [OK]
- Using struct instead of class
- Wrong AttributeTargets in AttributeUsage
- Missing constructor parameter for priority
Ready to prove it?
Pass the test and this pattern moves unknown
