Variables and Constants
A
variable
is a storage location with a type. In the preceding examples, both
x
and y
are
variables. Variables can have values assigned to them, and those
values can be changed programmatically.
Create a variable by declaring its type and then giving it a name. You can initialize the variable when you declare it, and you can assign a new value to that variable at any time, changing the value held in the variable. This is illustrated in Example 3-1.
Example 3-1. Initializing and assigning a value to a variable
class Values { static void Main( ) {int myInt = 7;
System.Console.WriteLine("Initialized, myInt: {0}", myInt);myInt = 5;
System.Console.WriteLine("After ...
Get Programming C#, Third Edition 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.