O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required