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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.