Challenge - 5 Problems
Value Type vs Reference Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of struct vs class assignment
Consider the following C# code. What will be the output when it runs?
C Sharp (C#)
struct PointStruct { public int X; public int Y; }
class PointClass { public int X; public int Y; }
var ps1 = new PointStruct { X = 5, Y = 10 };
var ps2 = ps1;
ps2.X = 20;
var pc1 = new PointClass { X = 5, Y = 10 };
var pc2 = pc1;
pc2.X = 20;
Console.WriteLine($"ps1.X = {ps1.X}, ps2.X = {ps2.X}");
Console.WriteLine($"pc1.X = {pc1.X}, pc2.X = {pc2.X}");Attempts:
2 left
💡 Hint
Remember that structs are value types and classes are reference types.
✗ Incorrect
Structs are copied by value, so changing ps2.X does not affect ps1.X.
Classes are copied by reference, so changing pc2.X also changes pc1.X.
❓ Predict Output
intermediate2:00remaining
Performance difference in method calls
What is the expected output of the following C# program that measures elapsed time for method calls on value and reference types?
C Sharp (C#)
struct MyStruct { public int Value; public void Increment() => Value++; }
class MyClass { public int Value; public void Increment() => Value++; }
var s = new MyStruct();
var c = new MyClass();
var sw = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < 10000000; i++) s.Increment();
sw.Stop();
var structTime = sw.ElapsedMilliseconds;
sw.Restart();
for (int i = 0; i < 10000000; i++) c.Increment();
sw.Stop();
var classTime = sw.ElapsedMilliseconds;
Console.WriteLine($"Struct time: {structTime} ms");
Console.WriteLine($"Class time: {classTime} ms");Attempts:
2 left
💡 Hint
Value types avoid heap allocation and indirection, which can improve performance.
✗ Incorrect
Struct method calls are generally faster because they avoid heap allocation and pointer dereferencing.
Class method calls involve reference indirection and possible heap overhead.
🔧 Debug
advanced2:00remaining
Why does this struct cause unexpected behavior?
Examine the following code. Why does the output show unexpected values for the struct fields after modification?
C Sharp (C#)
struct Data {
public int A;
public int B;
public void Update() {
A = 10;
B = 20;
}
}
Data d = new Data();
d.Update();
Console.WriteLine($"A = {d.A}, B = {d.B}");
Data d2 = d;
d2.A = 30;
Console.WriteLine($"d.A = {d.A}, d2.A = {d2.A}");Attempts:
2 left
💡 Hint
Think about how value types behave when assigned to new variables.
✗ Incorrect
Structs are copied on assignment, so d2 is a separate copy of d.
Changing d2.A does not affect d.A.
📝 Syntax
advanced2:00remaining
Identify the syntax error in struct initialization
Which option contains the correct syntax to initialize a struct with fields X=1 and Y=2?
C Sharp (C#)
struct Point { public int X; public int Y; }Attempts:
2 left
💡 Hint
Structs can be initialized with object initializer syntax.
✗ Incorrect
Option A uses correct object initializer syntax.
Options B and C are invalid because Point has no constructor defined.
Option A uses invalid syntax.
🚀 Application
expert3:00remaining
Choosing between struct and class for performance
You need to store millions of small 2D points with integer coordinates and perform many calculations. Which choice is best for performance and why?
Attempts:
2 left
💡 Hint
Think about memory layout and allocation overhead.
✗ Incorrect
Structs store data inline, reducing heap allocations and improving cache locality.
Classes have overhead due to heap allocation and references.
Inheritance is not needed here.