Thinking in C#: What This Simple Struct Challenge Teaches Us

At Coder Foundry, we don't just want our students to solve problems — we want them to understand why the code behaves the way it does. Today, we're breaking down a deceptively simple C# coding challenge that reveals an important lesson about value types, reference types, and how data flows through your application.
Let’s take a look at the challenge:
public struct MyData
{
public int Value { get; set; }
}
public static class Program
{
public static void AddValue(MyData data, int value)
{
data.Value += value;
}
public static void Main()
{
var data = new MyData { Value = 5 };
Console.WriteLine($"Value Before: {data.Value}");
// tweak this line to capture the return value
AddValue(data, 10);
Console.WriteLine($"Value After: {data.Value}");
}
}
Expected vs. Actual
Most new developers expect this program to output:
Value Before: 5
Value After: 15
But when you run it, you get:
Value Before: 5
Value After: 5
So… what happened?
The Struct Secret: Value Types Are Passed by Copy
In C#, struct types are value types, which means they are copied when passed into methods. This is different from classes, which are reference types.
That means when we call AddValue(data, 10), we’re modifying a copy of the data, not the original. So the changes are discarded when the method exits.
The Fix: Return the Modified Value
The challenge hint was “tweak this line to capture the return value.” That means we're meant to return the modified struct and reassign it to the original variable.
Step 1: Modify the Method
public static MyData AddValue(MyData data, int value)
{
data.Value += value;
return data;
}
Step 2: Capture the Return Value
data = AddValue(data, 10);
Result:
Value Before: 5
Value After: 15
Success!
Why This Challenge Matters
This challenge introduces one of the most important distinctions in C#: structs vs. classes, or in other words, value types vs. reference types. Understanding this helps you avoid bugs, write better code, and prepare for more advanced topics like memory management and performance optimization.
Let’s break it down even further.
Structs vs. Classes

Other Valid Solution
There’s more than one way to fix this code. Here’s another valid solution depending on your intent:
Option: Use ref to Pass by Reference
public static void AddValue(ref MyData data, int value)
{
data.Value += value;
}
AddValue(ref data, 10);
Use this when you want to modify the original value directly, without returning anything.
Final Thought
This challenge highlights one of the most important lessons new developers must learn: not all data is treated the same in C#. Structs may look like classes, but they behave very differently, and that difference can lead to subtle bugs if you're not paying attention.
When you're learning to code, challenges like this aren’t just about getting the right output. They're about building a deeper mental model of how your code actually works under the hood. If you walked away from this realizing why the original code didn’t behave as expected, you’re already thinking like a professional developer.
Keep exploring, keep asking "why?", and don’t be afraid to dig into the fundamentals — they’re what turn good coders into great ones.
Ready to Level Up Your Coding Skills?
Whether you're just getting started or looking to sharpen your C# expertise, Coder Foundry offers both full-time immersive bootcamps and flexible self-paced programs to help you launch a career in software development.
If you found this challenge helpful, imagine what you could accomplish with structured training, expert mentorship, and real-world projects under your belt.
Visit CoderFoundry.com to learn more and take the next step toward becoming a professional developer.