Variables

To store data, we need storage cells. Those are called variables and always have an associated type. A type-safe language like C# guarantees the values stored in such a variable have the appropriate type. If this weren’t the case, it would be possible to look at a Person instance as if it were a Fruit instance, something that doesn’t make sense and clearly indicates a code defect.

Variables can occur in various places. Actually, we’ve seen most of them already. The first one is as a local variable:

int value = discount.Value;

This declares a variable of type int and immediately assigns it some value by retrieving the Value property (or field, if bad coding style is used) from another variable called discount.

Where did this discount ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.