Skip to Content
Course content

157: Common C# Interview Questions on Value/Reference Semantics

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I remember working with a junior dev named Leo a few years back. He had spent nearly an entire afternoon staring at a piece of code that simply wouldn't work. He was using a struct to represent a 2D coordinate in a game, and he had a list of these coordinates. He wrote a method to move a coordinate by a certain offset, called the method, and then checked the list. The coordinates hadn't moved an inch. He was convinced there was a bug in the .NET List<T> implementation. The reality was much simpler: he was modifying a copy of the value, not the original instance. It's a rite of passage in C#, and it's exactly why interviewers love to grill you on value and reference semantics.

The Struct Trap and Copy-by-Value

When you're in an interview and they ask you the difference between a class and a struct, don't just say "one is a reference type and one is a value type." That's the textbook answer, but it doesn't show you actually understand it. Explain what happens in memory. Value types (like int, bool, and custom structs) are typically stored on the stack, and when you pass them into a method, C# creates a complete copy of that data.

public struct PlayerPoint { 
    public int X; 
    public int Y; 
}

public void MovePoint(PlayerPoint p) {
    p.X += 10; // This modifies a LOCAL copy
}

// Usage:
PlayerPoint myPoint = new PlayerPoint { X = 0, Y = 0 };
MovePoint(myPoint); 
Console.WriteLine(myPoint.X); // Still 0!

If you see this in an interview, point out that the MovePoint method received a copy of myPoint. To fix this, you'd use the ref keyword, which tells C# to pass the memory address of the variable rather than a copy of its value. I usually tell my mentees to be wary of mutable structs; if a struct needs to change, it's often a sign that it should have been a class all along.

Boxing and the Hidden Performance Hit

Another favorite interview topic is "Boxing." This happens when a value type is converted to a reference type—usually by casting it to an object or an interface. The runtime "boxes" the value type by wrapping it inside an object instance on the heap. Unboxing is the reverse process.

Why does this matter? Because boxing is expensive. It requires a new heap allocation and puts more pressure on the Garbage Collector (GC). If you're writing a high-performance loop and you're boxing an integer every single iteration, you're essentially killing your app's throughput. Interviewers want to see that you know List<int> (generic) is vastly superior to ArrayList (non-generic) precisely because the former avoids boxing by using generics.

Navigating the 'Reference' Trick Questions

You'll likely encounter a "trick" question involving strings. A string is a reference type, but it behaves like a value type because it is immutable. When you "change" a string, you aren't modifying the existing memory; you're creating a brand new string and updating the reference to point to it.

When an interviewer asks, "What happens if I pass a class instance to a method and change a property inside that method?", the answer is that the change persists. However, if you try to assign the entire object to a new instance inside that method, the original reference outside the method remains unchanged. It's a subtle distinction: you can change the contents of the object the reference points to, but you can't change which object the original reference points to unless you use the ref keyword.




📋 Practical Task

Fixing the Frozen Coordinate Bug

You have been handed a piece of code for a simple GPS tracker. The developer used a struct for the Location to save memory, but they've realized that the UpdateLocation method isn't actually updating the tracker's position. The location remains stuck at (0,0).

Your goal: Modify the UpdateLocation method signature and the way it is called in Main so that the Location is correctly updated in memory without changing the Location from a struct to a class.

using System;

public struct Location {
    public double Latitude;
    public double Longitude;
}

public class GpsTracker {
    public static void UpdateLocation(Location loc) {
        loc.Latitude = 34.0522;
        loc.Longitude = -118.2437;
    }

    public static void Main() {
        Location currentLoc = new Location { Latitude = 0, Longitude = 0 };
        
        Console.WriteLine($"Before: {currentLoc.Latitude}, {currentLoc.Longitude}");
        
        UpdateLocation(currentLoc);
        
        // This currently prints 0, 0. Make it print 34.0522, -118.2437
        Console.WriteLine($"After: {currentLoc.Latitude}, {currentLoc.Longitude}");
    }
}
Rating
0 0

There are no comments for now.

to be the first to leave a comment.