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

Attribute declaration syntax in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Attribute Syntax 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 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);
    }
}
ASample class
BInfoAttribute
CSystem.Attribute
Dnull
Attempts:
2 left
💡 Hint
Look at the Description property set by the attribute constructor.
Predict Output
intermediate
2: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() {}
}
ACS0246: The type or namespace name 'My' could not be found
BCS0106: The modifier 'public' is not valid for this item
CCS0592: Attribute 'My' is not valid on this declaration type. It is only valid on 'method' declarations.
DNo error, compiles successfully
Attempts:
2 left
💡 Hint
Check where the attribute is allowed to be applied versus where it is used.
🔧 Debug
advanced
2: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;
    }
}
AError: Duplicate constructor definitions with the same signature
BNo error, code compiles successfully
CError: Attribute constructors must have only one parameter
DError: Properties Min and Max must have setters
Attempts:
2 left
💡 Hint
Check if multiple constructors with different parameters are allowed in attributes.
Predict Output
advanced
2: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}");
    }
}
AName: Test, Version: 2
BName: , Version: 0
CName: Test, Version: 0
DName: , Version: 2
Attempts:
2 left
💡 Hint
Named parameters set properties after constructor runs.
📝 Syntax
expert
2:00remaining
Which attribute declaration is syntactically correct?
Choose the only syntactically correct attribute declaration in C#.
A
[AttributeUsage(AttributeTargets.Class, AllowMultiple = True)]
public class MultiUseAttribute : Attribute {}
B
[AttributeUsage(AttributeTargets.Class, AllowMultiple = "true")]
public class MultiUseAttribute : Attribute {}
C
[AttributeUsage(AttributeTargets.Class, AllowMultiple = 1)]
public class MultiUseAttribute : Attribute {}
D
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class MultiUseAttribute : Attribute {}
Attempts:
2 left
💡 Hint
Check the type of the AllowMultiple parameter in AttributeUsage.