📖 Scenario: You are creating a simple C# program to understand how to apply attributes to different parts of your code, like classes, methods, and properties. Attributes add extra information to your code that can be used by the compiler or at runtime.
🎯 Goal: Build a C# program that defines a custom attribute and applies it to a class, a method, and a property using correct attribute targets.
📋 What You'll Learn
Create a custom attribute class named InfoAttribute.
Apply InfoAttribute to a class, a method, and a property using correct attribute targets.
Use the AttributeUsage attribute to specify valid targets for InfoAttribute.
Print messages to confirm the attribute usage.
💡 Why This Matters
🌍 Real World
Attributes are used in real-world C# programs to add metadata for tools, frameworks, and runtime behaviors, like marking methods for testing or serialization.
💼 Career
Understanding attributes is important for working with many C# frameworks and libraries, such as ASP.NET, Entity Framework, and unit testing tools.
Progress0 / 4 steps
1
Create the custom attribute class
Create a public class called InfoAttribute that inherits from System.Attribute. Inside it, add a public string property called Description with a constructor that sets this property.
C Sharp (C#)
Hint
Remember, custom attributes inherit from System.Attribute. Use a constructor to set the description.
2
Specify attribute usage targets
Add the AttributeUsage attribute above the InfoAttribute class to allow it to be used on Class, Method, and Property targets.
C Sharp (C#)
Hint
Use AttributeTargets.Class, AttributeTargets.Method, and AttributeTargets.Property combined with the bitwise OR operator |.
3
Apply the attribute to class, method, and property
Create a public class called Product. Apply [Info("Class for product details")] to the class. Inside, add a public property Name of type string with [Info("Product name")] attribute. Also add a public method Display with [Info("Display product info")] attribute that prints the product name.
C Sharp (C#)
Hint
Apply the [Info(...)] attribute above the class, property, and method declarations.
4
Create instance and call method to show attribute usage
In the Main method, create an instance of Product, set its Name to "Laptop", and call the Display method to print the product name.
C Sharp (C#)
Hint
Create a Product object, set the Name property, then call Display() to print the name.
Practice
(1/5)
1. What does the AttributeUsage attribute control in C#?
easy
A. The visibility of a class
B. The return type of a method
C. Where an attribute can be applied (e.g., class, method, property)
D. The inheritance of a class
Solution
Step 1: Understand the purpose of AttributeUsage
The AttributeUsage attribute specifies the valid program elements (targets) where a custom attribute can be applied, such as classes, methods, or properties.
Step 2: Eliminate unrelated options
Options about return types, visibility, or inheritance are unrelated to attribute targets.
Final Answer:
Where an attribute can be applied (e.g., class, method, property) -> Option C
Quick Check:
AttributeUsage controls attribute targets = C [OK]
Hint: AttributeUsage sets where attributes can be placed [OK]
Common Mistakes:
Confusing AttributeUsage with method or class modifiers
Thinking it controls inheritance or visibility
Assuming it changes method return types
2. Which of the following is the correct syntax to allow an attribute to be applied only to methods?
easy
A. [AttributeUsage(AttributeTargets.Method)]
B. [AttributeUsage(AttributeTargets.Class)]
C. [AttributeUsage(AttributeTargets.Property)]
D. [AttributeUsage(AttributeTargets.Field)]
Solution
Step 1: Identify the target for methods
The AttributeTargets.Method enum value specifies that the attribute can only be applied to methods.
Step 2: Match the syntax
The correct syntax uses [AttributeUsage(AttributeTargets.Method)] to restrict usage to methods.
Final Answer:
[AttributeUsage(AttributeTargets.Method)] -> Option A
Quick Check:
Method target uses AttributeTargets.Method = B [OK]
Hint: Use AttributeTargets.Method to target methods only [OK]
Common Mistakes:
Using Class or Property targets instead of Method
Forgetting square brackets around AttributeUsage
Misspelling AttributeTargets
3. What will happen if you apply this attribute to a class?
[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute { }
[MyAttribute]
public class TestClass { }
medium
A. Compilation error because attribute is applied to a class, not a method
B. The attribute is applied successfully to the class
C. Runtime error when the program runs
D. Warning but code compiles
Solution
Step 1: Check the AttributeUsage target
The attribute MyAttribute is restricted to methods only by AttributeTargets.Method.
Step 2: Analyze the application
Applying [MyAttribute] to a class violates the target restriction, causing a compilation error.
Final Answer:
Compilation error because attribute is applied to a class, not a method -> Option A
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class InfoAttribute : Attribute { }
[Info]
[Info]
public class Sample { }
medium
A. No error, multiple attributes allowed
B. Error because AllowMultiple is false but attribute applied twice
C. Error because AttributeTargets is missing Property
D. Error because attribute class must be sealed
Solution
Step 1: Understand AllowMultiple property
The AllowMultiple = false means the attribute cannot be applied more than once on the same element.
Step 2: Check attribute usage on Sample class
The [Info] attribute is applied twice on the same class, violating the AllowMultiple restriction.
Final Answer:
Error because AllowMultiple is false but attribute applied twice -> Option B
5. You want to create an attribute that can be applied to classes, methods, and properties, and allow multiple uses on the same element. Which is the correct AttributeUsage declaration?
hard
A. [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
B. [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
C. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
D. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
Solution
Step 1: Combine targets for classes, methods, and properties
Use bitwise OR to combine AttributeTargets.Class, AttributeTargets.Method, and AttributeTargets.Property.
Step 2: Set AllowMultiple to true
To allow multiple uses on the same element, set AllowMultiple = true.