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

Custom attribute classes in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Attribute Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code using a custom attribute?

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?

C Sharp (C#)
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);
    }
}
ACompilation error
BSystem.InfoAttribute
Cnull
DThis is a sample class
Attempts:
2 left
💡 Hint

Look at how the attribute is retrieved and what property is printed.

🧠 Conceptual
intermediate
1:30remaining
Which attribute usage is valid for this custom attribute?

Given a custom attribute defined as below, which AttributeUsage setting correctly restricts it to be used only on methods?

C Sharp (C#)
using System;

[AttributeUsage(AttributeTargets.Method)]
public class MyMethodAttribute : Attribute
{
}
A[AttributeUsage(AttributeTargets.Method)]
B[AttributeUsage(AttributeTargets.Class)]
C[AttributeUsage(AttributeTargets.Property)]
D[AttributeUsage(AttributeTargets.Field)]
Attempts:
2 left
💡 Hint

Think about which AttributeTargets value limits usage to methods only.

🔧 Debug
advanced
2:30remaining
Why does this code throw an exception when retrieving the attribute?

Examine the code below. It throws a NullReferenceException at runtime. What is the cause?

C Sharp (C#)
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());
    }
}
AThe Product class does not have the TagAttribute applied, so attr is null causing NullReferenceException.
BThe TagAttribute constructor is missing a parameterless constructor causing the error.
CAttributeUsage is set incorrectly causing the attribute not to be found.
DThe cast to TagAttribute is invalid causing an InvalidCastException.
Attempts:
2 left
💡 Hint

Check if the attribute is actually applied to the class before retrieving it.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a custom attribute with a named property?

Which of the following code snippets correctly defines a custom attribute with a named property Version that can be set optionally?

Apublic class VersionAttribute : Attribute { public int Version; }
Bpublic class VersionAttribute : Attribute { public int Version { get; set; } }
Cpublic class VersionAttribute : Attribute { public int Version() { get; set; } }
Dpublic class VersionAttribute : Attribute { public int GetVersion() => 1; }
Attempts:
2 left
💡 Hint

Remember named properties in attributes must be public properties with get and set accessors.

🚀 Application
expert
3:00remaining
What is the output of this code using multiple custom attributes and reflection?

Given the code below, what will be printed when running the Main method?

C Sharp (C#)
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 + " ");
    }
}
ACompilation error due to multiple attributes
BCore Utility
CUtility Core
DUtilityCore
Attempts:
2 left
💡 Hint

Check the order attributes are declared and how they are retrieved.