Custom Attributes in C#

From the C# Programming Language cheat sheet · Attributes and Reflection · verified Jul 2026

Custom Attributes

Using and creating attributes for metadata

csharp
// Built-in attributes
[Obsolete("Use NewMethod instead")]
public void OldMethod() { }

[Serializable]
public class MyClass { }

// Custom attribute
[AttributeUsage(AttributeTargets.Property)]
public class RequiredAttribute : Attribute { }

// Using custom attribute
public class User
{
    [Required]
    public string Name { get; set; }
}
💡 Attributes add metadata to code elements
⚡ Reflection allows runtime type inspection and manipulation
📌 Custom attributes enable declarative programming
🟢 Use reflection sparingly as it impacts performance
Back to the full C# Programming Language cheat sheet