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

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

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

Practice

(1/5)
1. Which of the following is true about value types in C#?
easy
A. They cannot be used with primitive data like int or bool.
B. They store a reference to the data on the heap.
C. They are always slower than reference types.
D. They store data directly and are generally faster for small data.

Solution

  1. Step 1: Understand value type storage

    Value types store the actual data directly, usually on the stack, which makes access faster for small data.
  2. Step 2: Compare with reference types

    Reference types store a pointer to data on the heap, which involves extra memory access and can be slower.
  3. Final Answer:

    They store data directly and are generally faster for small data. -> Option D
  4. Quick Check:

    Value types = direct data storage [OK]
Hint: Value types hold data directly, making them faster for small data [OK]
Common Mistakes:
  • Confusing value types with reference types
  • Thinking value types always slower
  • Believing value types cannot hold primitives
2. Which syntax correctly declares a value type variable in C#?
easy
A. int number = 10;
B. string text = "hello";
C. MyClass obj = new MyClass();
D. var list = new List<int>();

Solution

  1. Step 1: Identify value type declaration

    In C#, int is a value type and int number = 10; declares it correctly.
  2. Step 2: Check other options

    string, MyClass, and List<int> are reference types, so they are incorrect for value types.
  3. Final Answer:

    int number = 10; -> Option A
  4. Quick Check:

    int is value type declaration [OK]
Hint: Value types use simple types like int, double, bool [OK]
Common Mistakes:
  • Choosing string as value type
  • Confusing class instances with value types
  • Using var without clear type
3. What is the output of this C# code?
struct Point { public int X; public int Y; }
Point p1 = new Point { X = 5, Y = 10 };
Point p2 = p1;
p2.X = 20;
Console.WriteLine(p1.X);
medium
A. 20
B. 0
C. 5
D. Compilation error

Solution

  1. Step 1: Understand struct copying behavior

    Structs are value types, so p2 = p1; copies the data, not the reference.
  2. Step 2: Analyze modification effect

    Changing p2.X to 20 does not affect p1.X, which remains 5.
  3. Final Answer:

    5 -> Option C
  4. Quick Check:

    Value type copy keeps original unchanged [OK]
Hint: Struct assignment copies data, original stays same [OK]
Common Mistakes:
  • Assuming structs behave like reference types
  • Expecting p1.X to change after p2.X changes
  • Confusing struct with class behavior
4. Identify the error in this code related to value and reference types:
class MyClass { public int Value; }
MyClass a = new MyClass();
MyClass b = a;
b.Value = 10;
Console.WriteLine(a.Value);
medium
A. No error; output is 10 because both refer to same object.
B. Error: Cannot assign one class instance to another.
C. Error: Value types cannot be assigned like this.
D. Output is 0 because b.Value change does not affect a.Value.

Solution

  1. Step 1: Understand reference type assignment

    Classes are reference types, so b = a; makes both variables point to the same object.
  2. Step 2: Analyze output after modification

    Changing b.Value to 10 changes the object, so a.Value also shows 10.
  3. Final Answer:

    No error; output is 10 because both refer to same object. -> Option A
  4. Quick Check:

    Reference types share object, changes reflect [OK]
Hint: Class variables point to same object, changes affect both [OK]
Common Mistakes:
  • Thinking class assignment copies data
  • Expecting separate copies for reference types
  • Confusing value and reference type behavior
5. You want to improve performance by reducing heap allocations in a C# program that uses many small data objects. Which approach is best?
hard
A. Use classes everywhere to benefit from garbage collection.
B. Use structs (value types) instead of classes (reference types) for small data objects.
C. Use strings instead of structs for better speed.
D. Always box value types to improve memory usage.

Solution

  1. Step 1: Identify performance impact of types

    Classes allocate objects on the heap, causing more memory overhead and garbage collection.
  2. Step 2: Choose value types for small data

    Structs are value types stored on the stack, reducing heap allocations and improving speed for small objects.
  3. Final Answer:

    Use structs (value types) instead of classes (reference types) for small data objects. -> Option B
  4. Quick Check:

    Structs reduce heap allocations, improving performance [OK]
Hint: Use structs for small data to avoid heap overhead [OK]
Common Mistakes:
  • Using classes for all data regardless of size
  • Thinking boxing improves memory
  • Replacing structs with strings incorrectly
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