Programmer Puzzle - Class and struct
Article Index
Programmer Puzzle - Class and struct
Solution

Solution

The reason the method no longer works is that struct isn't just a lightweight form of class. A struct is a value type and this means that value rather than reference semantics apply. For example:

Mystruct2=Mystruct1;

where both are structs results in a complete and separate copy of Mystruct1 being stored in Mystruct2. 

However:

Myclass2=Myclass1;

where both are classes results in Myclass2 referencing the same object as Myclass1, i.e. there is not separate copy created of the object.

In the same way when you pass a value type into a method you pass a complete new copy which has nothing to do with the original. For example if mymethod is:

void mymethod(int i)
{
i=3;
}

then the result of the call:

i=2;
mymethod(i);

is that i is still 2.

However if i is part of a  reference type:

public class myclass
{
public int i;
}

and mymethod is modified to read:

public void mymethod(myclass O)
{
O.i = 4;
}

then after

myclass myobject = new myclass() { i = 3 };
mymethod(myobject);

i is changed to 4.

The behavior changes depending on whether or not you are passing a value or a reference type.

In the same way in our puzzle:

public void ZeroPoint(point p)
{       
p.x=0;
p.y=0;
}

doesn't change the values of x and y on the value type passed into the method but on a copy of the value type used within the method.

By contrast if point is a reference type, then p is a reference to the object passed in and, in this case, the changes are made on the original object and its x and y properites are changed.

 

Pattern

There is no easy solution to this problem if you want to write methods like ZeroPoint that make changes to objects passed as parameters - apart from being very aware of the difference between value and reference semantics.

You can make mymethod work in the same way as for a value type as for a reference type by changing the use to pass by reference:

public void ZeroPoint(ref point p)
{
p.x = 0;
p.y = 0;
}

but this also requires a change to every use of ZeroPoint to:

ZeroPoint(ref p1);

Another approach is to  test to see if the passed-in parameter was a value type and throw an exception if it was. But this would add an overhead for a rarely encountered event, i.e that the programmer changes a class to as struct or vice versa.

A more sensible approach is not to use methods that make changes to objects that are passed via parameters. Such changes are called "side effects" and it is good programming practice to avoid changes via side effects. 

A good method should change nothing and pass back a result.

This takes us deep into functional programming and other interesting ideas.

Further reading:

Inside C# 4 Data Structs

Value and Reference

Banner

More Puzzles

Sharpen Your Coding Skills
Towers Of Hanoi Mutants

Towers of Hanoi is a classic puzzle and is often used to illustrate the idea of recursion. Here you are challenged to find solutions to some variations, after first explaining the original version.


Javascript
Stringy Objects

A Programmer's Puzzle in which we contemplate situations in which string equivalence in Javascript is clearly not what it seems - and explain why it all goes wrong.


C#
The Inheritance Problem

Inheritance and function overloading don't play well together in C#. See if you can figure out the reasons that this puzzle doesn't work as you might expect.


Other Articles