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

Readonly structs in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Readonly Structs Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of readonly struct method call
What is the output of this C# code using a readonly struct?
C Sharp (C#)
readonly struct Point
{
    public int X { get; }
    public int Y { get; }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public int Sum() => X + Y;
}

class Program
{
    static void Main()
    {
        Point p = new Point(3, 4);
        System.Console.WriteLine(p.Sum());
    }
}
A7
B0
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Think about what the Sum method returns and how readonly structs behave.
Predict Output
intermediate
2:00remaining
Effect of modifying field in readonly struct
What happens when you try to modify a field inside a readonly struct method?
C Sharp (C#)
readonly struct Counter
{
    private int count;

    public void Increment()
    {
        count++;
    }

    public int GetCount() => count;
}

class Program
{
    static void Main()
    {
        Counter c = new Counter();
        c.Increment();
        System.Console.WriteLine(c.GetCount());
    }
}
AOutput: 0
BOutput: 1
CCompilation error: cannot modify field in readonly struct
DRuntime exception
Attempts:
2 left
💡 Hint
Readonly structs prevent modification of fields inside instance methods.
🔧 Debug
advanced
2:00remaining
Why does this readonly struct code fail to compile?
Identify the reason for the compilation error in this readonly struct code.
C Sharp (C#)
readonly struct Rectangle
{
    public int Width { get; set; }
    public int Height { get; set; }

    public int Area() => Width * Height;
}

class Program
{
    static void Main()
    {
        Rectangle r = new Rectangle { Width = 5, Height = 10 };
        System.Console.WriteLine(r.Area());
    }
}
AArea method cannot access properties in readonly struct
BNo compilation error
CCannot initialize readonly struct with object initializer
DReadonly structs cannot have set accessors in properties
Attempts:
2 left
💡 Hint
Think about what readonly means for property setters in structs.
📝 Syntax
advanced
2:00remaining
Which option correctly declares a readonly struct with an immutable field?
Choose the correct syntax to declare a readonly struct with an immutable field 'Id' of type int.
Areadonly struct User { public readonly int Id; public User(int id) { Id = id; } }
Breadonly struct User { public int Id; public User(int id) { Id = id; } }
Creadonly struct User { int Id; public User(int id) { Id = id; } }
Dstruct User { readonly int Id; public User(int id) { Id = id; } }
Attempts:
2 left
💡 Hint
Remember to use 'public readonly' for immutable fields in readonly structs.
🚀 Application
expert
2:00remaining
How many items are in the resulting list after processing readonly structs?
Given this code, how many elements does the list 'areas' contain after execution?
C Sharp (C#)
readonly struct Square
{
    public int Side { get; }
    public Square(int side) => Side = side;
    public int Area() => Side * Side;
}

class Program
{
    static void Main()
    {
        var squares = new Square[] { new(2), new(3), new(4) };
        var areas = new System.Collections.Generic.List<int>();
        foreach (var sq in squares)
        {
            areas.Add(sq.Area());
        }
        System.Console.WriteLine(areas.Count);
    }
}
A0
B3
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Count how many squares are in the array and how many times the loop runs.