Challenge - 5 Problems
Attribute Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of attribute usage on method
What is the output when the following C# program runs?
C Sharp (C#)
using System; [AttributeUsage(AttributeTargets.Method)] public class InfoAttribute : Attribute { public string Text { get; } public InfoAttribute(string text) => Text = text; } class Program { [Info("Test method")] public static void Display() => Console.WriteLine("Hello World"); static void Main() { var method = typeof(Program).GetMethod("Display"); var attr = (InfoAttribute)Attribute.GetCustomAttribute(method, typeof(InfoAttribute)); Console.WriteLine(attr.Text); Display(); } }
Attempts:
2 left
💡 Hint
Look at how the attribute is retrieved and printed before calling the method.
✗ Incorrect
The attribute InfoAttribute is applied to the method Display. The program retrieves this attribute and prints its Text property first, then calls Display which prints "Hello World".
❓ Predict Output
intermediate2:00remaining
Attribute usage on class and property
What will be the output of this C# code?
C Sharp (C#)
using System; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)] public class LabelAttribute : Attribute { public string Name { get; } public LabelAttribute(string name) => Name = name; } [Label("Person Class")] class Person { [Label("Person Name")] public string Name { get; set; } } class Program { static void Main() { var classAttr = (LabelAttribute)Attribute.GetCustomAttribute(typeof(Person), typeof(LabelAttribute)); var propAttr = (LabelAttribute)Attribute.GetCustomAttribute(typeof(Person).GetProperty("Name"), typeof(LabelAttribute)); Console.WriteLine(classAttr.Name); Console.WriteLine(propAttr.Name); } }
Attempts:
2 left
💡 Hint
Check which targets the attribute is allowed on and where it is applied.
✗ Incorrect
The LabelAttribute is applied to both the Person class and its Name property. The program retrieves and prints both attribute names in order.
❓ Predict Output
advanced2:00remaining
Effect of AttributeUsage AllowMultiple = true
What is the output of this program?
C Sharp (C#)
using System; using System.Linq; [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class TagAttribute : Attribute { public string Name { get; } public TagAttribute(string name) => Name = name; } [Tag("First")] [Tag("Second")] class Sample {} class Program { static void Main() { var attrs = Attribute.GetCustomAttributes(typeof(Sample), typeof(TagAttribute)).Cast<TagAttribute>(); foreach(var attr in attrs) Console.WriteLine(attr.Name); } }
Attempts:
2 left
💡 Hint
AllowMultiple = true allows multiple attributes of the same type on one target.
✗ Incorrect
Because AllowMultiple is true, both Tag attributes are applied and retrieved in the order they appear.
❓ Predict Output
advanced2:00remaining
Attribute usage with invalid target
What error does this code produce when compiled?
C Sharp (C#)
using System; [AttributeUsage(AttributeTargets.Method)] public class TestAttribute : Attribute {} [Test] class Demo { public void Run() {} }
Attempts:
2 left
💡 Hint
Check the AttributeTargets specified and where the attribute is applied.
✗ Incorrect
TestAttribute is allowed only on methods but is applied to a class, causing a compilation error.
🧠 Conceptual
expert2:00remaining
Determining valid attribute targets
Given this attribute declaration, which of the following targets can it be applied to without error?
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Parameter)]
public class ValidateAttribute : Attribute {}
Attempts:
2 left
💡 Hint
Look carefully at the AttributeTargets flags used.
✗ Incorrect
The attribute can be applied to fields, properties, and parameters only. Methods and classes are not allowed targets.