Consider the following C# code that defines a custom attribute and uses it on a class. What will be printed when running the Main method?
using System; [AttributeUsage(AttributeTargets.Class)] public class InfoAttribute : Attribute { public string Description { get; } public InfoAttribute(string description) => Description = description; } [Info("This is a sample class")] public class SampleClass {} public class Program { public static void Main() { var attr = (InfoAttribute)Attribute.GetCustomAttribute(typeof(SampleClass), typeof(InfoAttribute)); Console.WriteLine(attr.Description); } }
Look at how the attribute is retrieved and what property is printed.
The InfoAttribute is applied to SampleClass with the description "This is a sample class". The code retrieves this attribute instance and prints its Description property, so the output is exactly that string.
Given a custom attribute defined as below, which AttributeUsage setting correctly restricts it to be used only on methods?
using System; [AttributeUsage(AttributeTargets.Method)] public class MyMethodAttribute : Attribute { }
Think about which AttributeTargets value limits usage to methods only.
The AttributeTargets.Method restricts the attribute usage to methods only. Other options restrict usage to classes, properties, or fields, which are not correct for this attribute.
Examine the code below. It throws a NullReferenceException at runtime. What is the cause?
using System; [AttributeUsage(AttributeTargets.Class)] public class TagAttribute : Attribute { public string Name { get; } public TagAttribute(string name) => Name = name; } public class Product {} public class Program { public static void Main() { var attr = (TagAttribute)Attribute.GetCustomAttribute(typeof(Product), typeof(TagAttribute)); Console.WriteLine(attr.Name.ToUpper()); } }
Check if the attribute is actually applied to the class before retrieving it.
The Product class does not have the TagAttribute applied. So GetCustomAttribute returns null. Then calling attr.Name causes a NullReferenceException.
Which of the following code snippets correctly defines a custom attribute with a named property Version that can be set optionally?
Remember named properties in attributes must be public properties with get and set accessors.
Option B correctly defines a public property Version with get and set accessors, allowing it to be set optionally when using the attribute. Option B uses a field which is not allowed for named parameters. Option B has invalid syntax. Option B defines a method, not a property.
Given the code below, what will be printed when running the Main method?
using System; using System.Reflection; [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class TagAttribute : Attribute { public string Name { get; } public TagAttribute(string name) => Name = name; } [Tag("Utility")] [Tag("Core")] public class Helper {} public class Program { public static void Main() { var attrs = (TagAttribute[])Attribute.GetCustomAttributes(typeof(Helper), typeof(TagAttribute)); foreach (var attr in attrs) Console.Write(attr.Name + " "); } }
Check the order attributes are declared and how they are retrieved.
The Helper class has two TagAttribute instances applied: first "Utility", then "Core". GetCustomAttributes returns them in declaration order. The code prints each Name followed by a space, so output is "Utility Core " exactly.