0
0
C Sharp (C#)programming~20 mins

Attribute targets and usage in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Attribute Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
    }
}
ATest method\nHello World
BHello World\nTest method
CHello World
DCompilation error
Attempts:
2 left
💡 Hint
Look at how the attribute is retrieved and printed before calling the method.
Predict Output
intermediate
2: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);
    }
}
ACompilation error
BPerson Name\nPerson Class
CPerson Class\nPerson Name
DPerson Class
Attempts:
2 left
💡 Hint
Check which targets the attribute is allowed on and where it is applied.
Predict Output
advanced
2: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);
    }
}
ACompilation error due to multiple attributes
BFirst
CSecond\nFirst
DFirst\nSecond
Attempts:
2 left
💡 Hint
AllowMultiple = true allows multiple attributes of the same type on one target.
Predict Output
advanced
2: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() {}
}
ACompilation error: Attribute 'Test' is not valid on class
BNo error, compiles successfully
CWarning but compiles
DRuntime error: Invalid attribute usage
Attempts:
2 left
💡 Hint
Check the AttributeTargets specified and where the attribute is applied.
🧠 Conceptual
expert
2: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 {}
AOnly a property and a class
BA field, a property, and a method parameter
COnly a field and a method
DA method, a property, and a class
Attempts:
2 left
💡 Hint
Look carefully at the AttributeTargets flags used.