Mid
From PDF
Power Questions
High-Impact Interview Questions
Custom Attributes in C#?
Short answer: Real Use Cases Validation frameworks Logging metadata Role-based security API documentation metadata Custom Attribute Example [AttributeUsage(AttributeTargets.Property)] public class RequiredAttribute : Attribute {} Usage Example public class Employee
Example code
{ [Required] public string Name { get; set; }
} Validation Logic public static void Validate(object obj)
{
var properties = obj.GetType().GetProperties();
foreach (var prop in properties)
{
var isRequired = prop.GetCustomAttributes(typeof(RequiredAttribute), false).Any();
if (isRequired && prop.GetValue(obj) == null) throw new Exception($"{prop.Name} is required"); }
}
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png