Challenge - 5 Problems
Attribute Syntax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this attribute usage?
Consider the following C# code with a custom attribute applied to a class. What will be printed when the program runs?
C Sharp (C#)
using System; [AttributeUsage(AttributeTargets.Class)] public class InfoAttribute : Attribute { public string Description { get; } public InfoAttribute(string description) => Description = description; } [Info("Sample class")] public class Sample {} class Program { static void Main() { var attr = (InfoAttribute)Attribute.GetCustomAttribute(typeof(Sample), typeof(InfoAttribute)); Console.WriteLine(attr.Description); } }
Attempts:
2 left
💡 Hint
Look at the Description property set by the attribute constructor.
✗ Incorrect
The InfoAttribute is applied to the Sample class with the description "Sample class". Retrieving the attribute and printing its Description property outputs "Sample class".
❓ Predict Output
intermediate2:00remaining
What error occurs with this attribute declaration?
What error will this C# code produce when compiled?
C Sharp (C#)
[AttributeUsage(AttributeTargets.Method)] public class MyAttribute : Attribute { public int Value { get; set; } } [My(Value = 5)] public class TestClass { public void TestMethod() {} }
Attempts:
2 left
💡 Hint
Check where the attribute is allowed to be applied versus where it is used.
✗ Incorrect
The attribute My is declared to be valid only on methods, but it is applied to a class. This causes a compilation error CS0592.
🔧 Debug
advanced2:00remaining
Why does this attribute constructor cause a compilation error?
Identify the reason for the compilation error in this attribute declaration code.
C Sharp (C#)
public class RangeAttribute : Attribute { public int Min { get; } public int Max { get; } public RangeAttribute(int min, int max) { Min = min; Max = max; } public RangeAttribute(int min) { Min = min; Max = 100; } }
Attempts:
2 left
💡 Hint
Check if multiple constructors with different parameters are allowed in attributes.
✗ Incorrect
Attributes can have multiple constructors with different parameters. Properties can be read-only if set in constructor. This code compiles fine.
❓ Predict Output
advanced2:00remaining
What is the output of this attribute with named parameters?
What will this program print when run?
C Sharp (C#)
using System; [AttributeUsage(AttributeTargets.Class)] public class InfoAttribute : Attribute { public string Name { get; set; } public int Version { get; set; } } [Info(Name = "Test", Version = 2)] public class Demo {} class Program { static void Main() { var attr = (InfoAttribute)Attribute.GetCustomAttribute(typeof(Demo), typeof(InfoAttribute)); Console.WriteLine($"Name: {attr.Name}, Version: {attr.Version}"); } }
Attempts:
2 left
💡 Hint
Named parameters set properties after constructor runs.
✗ Incorrect
The attribute sets Name and Version via named parameters. These values are printed exactly.
📝 Syntax
expert2:00remaining
Which attribute declaration is syntactically correct?
Choose the only syntactically correct attribute declaration in C#.
Attempts:
2 left
💡 Hint
Check the type of the AllowMultiple parameter in AttributeUsage.
✗ Incorrect
AllowMultiple expects a boolean value true or false without quotes and with lowercase 'true'. Only option D uses the correct syntax.