C Sharp Variables and Constants

From Techotopia
Revision as of 16:14, 11 January 2008 by Neil (Talk | contribs) (New page: It is impossible gain proficiency in any programming language without first learning some the basics. Perhaps the most basic aspect of programming involves the use of variables and constan...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

It is impossible gain proficiency in any programming language without first learning some the basics. Perhaps the most basic aspect of programming involves the use of variables and constants. Even the most advanced and impressive programs from high end video games to enterprise commerce applications use variables in one form or another. In this chapter of C Sharp Essentials we will cover everything that a C# programmer needs to know about variables and constants.

What is a C# Variable?

Variables are essentially locations in computer memory that is reserved for storing the data used by an application. Each variable is assigned a name by the programmer and assigned a value. The name assigned to the variable may then be used in the C# code to access the value assigned to the variable. This access can involve either reading the value the variable, or changing the value. It is, of course, ability to change the value of a variable that gives them the name variable.

A variable must be declared as a particular type such as an integer, a character or a string. C# is what is known as a strongly typed' language in that once a variable has been declared as a particular type it cannot subsequently be changed to a different type. While this may come as a shock to those familiar with loosely typed languages such as Ruby it will be familiar to Java, C and C++ programmers. Whilst it is not possible to change the type of a variable it is possible to disguise the variable as another type under certain circumstances. This involves a concept known as casting and will be covered later in this chapter.

What is a C# Constant?