/ Express yourself

Express yourself - Variables and values

Variables and values are intimately intertwined. In mathematics, a variable describes an alphabetic character used to represent a number, whereas the value is that number. In programming, the meaning is very similar, a variable is a symbolic name that represents information, whereas the value is that information.

When we look at C#, we can break down variables and values further. A variable can be a reference type or (please don't get confused here) a value type. In a reference type, the value is a reference to a memory address, whereas the value type variable is a value of something tangible like the number one.

Value Type

There is not much more to know about for this type for now. The only other thing that I would like to point out is that when you copy between variables the new value is a copy of the original one, this will make more sense after we understand more about the reference type.

var valueTypaA = 1;
// valueTypaA is the variable and 1 is the value.

var valueTypaB = a;
// valueTypaB is the variable and 1 is the value.

Reference Type

As described above, a reference type is a reference to a memory address, but what exactly does that mean? Well at that memory address there is something special, an object, and because we only hold a reference, more than one variable can have that exact object as it's referred value.

var refTypeA = new object();
var refTypeB = refTypeA;
//The variables refTypeA and refTypeB both hold a reference to the same object.

It might not be immediately obvious why you want a variable referencing an object, let alone two variables but think of it this way.

Imagine you own a cafe, let's call this 'Cafe Bens', and are situated on a corner in a shopping complex, because of this corner you have two menu boards, one for people walking from the left and one for people walking from the right. Currently, you have two blackboards to fulfil this function, it's up to your son Joseph to climb up onto a ladder and manually write out the menu on both of them when it changes.

One day he comes in, after a week of daily menu alterations, and tells you he is sick of making all the changes and suggests you buy two LED TVs. He will replace the blackboards with the TVs and create a small menu web page that they can display, this way he only needs to make the change once and it will display for people approaching from either direction. It also has the added benefit where he does not have to climb up a ladder.

In the above story, we have analogies for both of our types. The blackboards can be thought of as value types, and the screens displaying the web page is the reference types. The blackboards, once a copy is made, are independent of each other's changes, if you need them to be the same you need to perform the same actions on both; where with the web page a single change will update what both TVs display. The removal of the ladder from the equation also has some meaning, Joseph is lazy ;)

The below is how a program might look when Joseph is climbing the ladder each day.

var leftBlackboard = "Today's special is Tuna";
var rightBlackboard = "Today's special is Tuna";

System.Console.WriteLine($"You ask Joseph to change the special to Salad.");
System.Console.WriteLine($"He complains that you don't make friends that way, but climbs the ladder and makes the change to leftBlackboard anyway.");

leftBlackboard = "Today's special is Salad";

System.Console.WriteLine($"leftBlackboard is now '{leftBlackboard}'.");
System.Console.WriteLine($"rightBlackboard is unchanged '{rightBlackboard}'.");

System.Console.WriteLine($"Joseph climbs the ladder again and makes the change to rightBlackboard.");

rightBlackboard  = "Today's special is Salad";

System.Console.WriteLine($"rightBlackboard is now '{rightBlackboard}' also.");

You can have a play with this code here.

Now let's look at what happens when you have installed the TVs.

var menu = new { Special = "Today's special is Tuna"};

var leftTv = menu.Special;
var rightTv = menu.Special;

System.Console.WriteLine($"You ask Joseph to change the special to Salad.");
System.Console.WriteLine($"He complains that you don't make friends that way, but sits down and makes the change to the menu web page anyway.");

menu = new { Special = "Today's special is Salad"};

System.Console.WriteLine($"leftTv is now '{leftTv}'.");
System.Console.WriteLine($"rightTv is now '{rightTv}'.");

System.Console.WriteLine($"Joseph puts the ladder in the bin.");

You can have a play with this code here.

in the above code block you will see:

var menu = new { Special = "Today's special is Tuna"};

This is just a way of creating an object in a line, a bit of syntax sugar that I am sure I will explain at some point.

Please feel free to ask anything, point out where I am wrong, or suggest improvements. My main aim is to consolidate my understanding of programming from a lens of C#.

In our next instalment Constants, Joe uses his woodworking skills.

Edits:

  • Added in code example for Joseph climbing the ladder and then throwing it in the bin, shout out to carlocruz for the idea.