By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Attributes in C# are a powerful feature that allows you to add metadata to your code. This metadata can be used by the runtime, compilers, or other tools to change the behavior of your program. Understanding attributes is crucial for tasks like serialization, obsolete marking, and creating custom behaviors. Misusing attributes can lead to runtime errors, inefficient code, or missed opportunities for optimization. For instance, failing to mark a class as [Serializable] can prevent it from being serialized, leading to data loss or corruption.
csharp [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public class MyCustomAttribute : Attribute { public string Description { get; set; } }
⚠️ Common Pitfall: Forgetting to specify AttributeTargets can lead to misapplication.
Apply an Attribute
csharp [MyCustomAttribute(Description = "This is a custom attribute")] public class MyClass { // Class implementation }
⚠️ Common Pitfall: Incorrect syntax can cause compilation errors.
Use the [Obsolete] Attribute
csharp [Obsolete("Use NewMethod instead", false)] public void OldMethod() { // Method implementation }
⚠️ Common Pitfall: Setting the second parameter to true makes it a compile-time error, which can break builds.
Use the [Serializable] Attribute
csharp [Serializable] public class MySerializableClass { public int MyProperty { get; set; } }
⚠️ Common Pitfall: Not all types can be serialized; check for serializable members.
Access Attributes via Reflection
csharp var attributes = typeof(MyClass).GetCustomAttributes(typeof(MyCustomAttribute), false); foreach (MyCustomAttribute attr in attributes) { Console.WriteLine(attr.Description); }
Experts view attributes as a way to decouple metadata from business logic. They think of attributes as a contract that defines how code should behave, making the codebase more maintainable and extensible. Instead of hardcoding behaviors, they use attributes to dynamically adjust functionality.
Exam trap: Questions may ask about the default behavior when AttributeTargets is not specified.
The mistake: Incorrectly using the [Obsolete] attribute.
Exam trap: Questions may test your understanding of the parameters.
The mistake: Marking non-serializable types with [Serializable].
Exam trap: Questions may involve identifying serializable types.
The mistake: Overusing reflection.
Scenario: You need to mark a method as obsolete and provide a warning message.Question: How do you apply the [Obsolete] attribute correctly? Solution: 1. Apply the [Obsolete] attribute to the method.2. Provide a warning message and set the second parameter to false.Answer:
[Obsolete("Use NewMethod instead", false)] public void OldMethod() { // Method implementation }
Why it works: The attribute provides a clear warning to users without causing a compile-time error.
Scenario: You need to serialize a class but encounter runtime errors.Question: What might be the issue? Solution: 1. Check if the class is marked with [Serializable].2. Verify that all members of the class are serializable.Answer:
[Serializable] public class MySerializableClass { public int MyProperty { get; set; } }
Why it works: Marking the class and its members as serializable allows for successful serialization.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.