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

Value type vs reference type performance in C Sharp (C#) - Practice Questions

Choose your learning style9 modes available
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
intermediate
2: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}");
A
ps1.X = 20, ps2.X = 20
pc1.X = 5, pc2.X = 20
B
ps1.X = 5, ps2.X = 20
pc1.X = 20, pc2.X = 20
C
ps1.X = 5, ps2.X = 5
pc1.X = 5, pc2.X = 5
D
ps1.X = 20, ps2.X = 5
pc1.X = 20, pc2.X = 5
Attempts:
2 left
💡 Hint
Remember that structs are value types and classes are reference types.
Predict Output
intermediate
2: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");
AStruct time: less than Class time
BProgram throws a runtime exception
CStruct time: equal to Class time
DStruct time: greater than Class time
Attempts:
2 left
💡 Hint
Value types avoid heap allocation and indirection, which can improve performance.
🔧 Debug
advanced
2: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}");
ABecause the struct fields are private and cannot be changed
BBecause structs are reference types, d2 and d refer to the same object
CBecause Update() method does not modify the struct fields
DBecause structs are value types, d2 is a copy; modifying d2.A does not affect d.A
Attempts:
2 left
💡 Hint
Think about how value types behave when assigned to new variables.
📝 Syntax
advanced
2: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; }
Avar p = new Point { X = 1, Y = 2 };
Bvar p = Point(1, 2);
Cvar p = new Point(1, 2);
Dvar p = new Point[X=1, Y=2];
Attempts:
2 left
💡 Hint
Structs can be initialized with object initializer syntax.
🚀 Application
expert
3: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?
AUse class because it is faster to copy than struct
BUse class because it allows inheritance and polymorphism
CUse struct because it stores data inline and reduces heap allocations
DUse struct because it is always faster regardless of size
Attempts:
2 left
💡 Hint
Think about memory layout and allocation overhead.