Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the correct way to declare an attribute named Serializable above a class in C#?
easy
A. [Serializable] public class MyClass { }
B. Serializable public class MyClass { }
C. (Serializable) public class MyClass { }
D. {Serializable} public class MyClass { }

Solution

  1. Step 1: Understand attribute syntax

    Attributes in C# are declared using square brackets [] placed above the code element.
  2. Step 2: Apply correct syntax for class attribute

    The attribute Serializable must be enclosed in square brackets before the class declaration.
  3. Final Answer:

    [Serializable] public class MyClass { } -> Option A
  4. Quick Check:

    Attributes use [] brackets = A [OK]
Hint: Attributes always use square brackets [] above declarations [OK]
Common Mistakes:
  • Using parentheses () instead of square brackets
  • Omitting brackets entirely
  • Using curly braces {} instead of brackets
2. Which of the following is the correct syntax to declare an attribute with a parameter "Test" for Obsolete?
easy
A. [Obsolete("Test")]()
B. [Obsolete: "Test"]
C. [Obsolete = "Test"]
D. [Obsolete("Test")]

Solution

  1. Step 1: Recall attribute parameter syntax

    Attribute parameters are passed inside parentheses immediately after the attribute name.
  2. Step 2: Identify correct parameter passing

    The string parameter "Test" must be inside parentheses without extra symbols or colons.
  3. Final Answer:

    [Obsolete("Test")] -> Option D
  4. Quick Check:

    Parameters go inside () after attribute name [OK]
Hint: Use parentheses () for parameters inside square brackets [] [OK]
Common Mistakes:
  • Using colons or equals signs instead of parentheses
  • Adding extra parentheses after the attribute
  • Placing parameters outside brackets
3. What will be the output of the following code?
[Obsolete("Use NewMethod instead")]
public void OldMethod() { Console.WriteLine("Old"); }

public void NewMethod() { Console.WriteLine("New"); }

OldMethod();
medium
A. New
B. Use NewMethod instead
C. Old
D. Compilation error

Solution

  1. Step 1: Understand the Obsolete attribute effect

    The Obsolete attribute marks a method as outdated but does not prevent it from running.
  2. Step 2: Analyze method call output

    Calling OldMethod() will execute its body and print "Old" to the console.
  3. Final Answer:

    Old -> Option C
  4. Quick Check:

    Obsolete warns but runs method = Old [OK]
Hint: Obsolete warns but method still runs unless error set [OK]
Common Mistakes:
  • Thinking Obsolete stops method execution
  • Expecting warning text as output
  • Confusing method names in output
4. Identify the error in this attribute declaration:
[Obsolete("Deprecated"]
public void Old() { }
medium
A. Missing closing parenthesis in attribute
B. Missing closing square bracket in attribute
C. Attribute name misspelled
D. Method missing return type

Solution

  1. Step 1: Check attribute brackets and parentheses

    The attribute starts with [Obsolete("Deprecated"] but the parentheses are not closed properly.
  2. Step 2: Identify missing closing parenthesis

    The string parameter "Deprecated" is opened with a parenthesis but lacks the closing ) before the closing square bracket.
  3. Final Answer:

    Missing closing parenthesis in attribute -> Option A
  4. Quick Check:

    Parentheses must close before square bracket [OK]
Hint: Check matching () inside [] for attributes [OK]
Common Mistakes:
  • Forgetting to close parentheses before square bracket
  • Confusing square brackets with parentheses
  • Assuming method return type causes attribute error
5. You want to create a custom attribute Info that takes a string parameter description. Which is the correct way to declare it?
hard
A. public attribute Info(string description) { }
B. public class InfoAttribute : Attribute { public InfoAttribute(string description) { } }
C. [Info(string description)] public class MyClass { }
D. public class Info : Attribute { public void Info(string description) { } }

Solution

  1. Step 1: Understand custom attribute class declaration

    Custom attributes must be classes derived from Attribute and named with Attribute suffix.
  2. Step 2: Define constructor with parameter

    The constructor takes the string parameter description to accept data when used.
  3. Final Answer:

    public class InfoAttribute : Attribute { public InfoAttribute(string description) { } } -> Option B
  4. Quick Check:

    Custom attribute = class derived from Attribute with constructor [OK]
Hint: Custom attributes are classes ending with Attribute and inherit Attribute [OK]
Common Mistakes:
  • Using 'attribute' keyword instead of class
  • Trying to declare attribute parameters in square brackets
  • Defining method instead of constructor for parameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard