Variables and Assignment Statements

This section describes variables, variable declaration statements, and assignment statements.

Literals help us to enter data into C# programs where it is needed for processing. If the same data is needed in two places, then we can enter the data into two places. But this is not efficient. We need a way to store data into computer memory, and retrieve it back later. This is done through the use of variables.

Variable represents an area in the computer memory where a value of a particular data type can be stored. The stored value can then be retrieved later by referring to the name of the variable.

Variables are divided into different types based on the type and the size of data to be stored. Each variable type has its own reserved key word in C# which is used to define variables in that type. The following is a list of key words for some commonly used variable types:

Each variable must have a unique name. Variable name must satisfy the following rule.

Rule: Variable name must be a sequence of alphabetical and numerical characters. No space character is allowed. No numerical character is allowed as the leading character of the name. Underscore character '_' is allowed in a variable name.

The name and type of a variable is defined by a variable declaration statement, with the following syntax:

variable_declariation_statement:
   variable_type_key_word variable_name;
   variable_type_key_word variable_name_1, variable_name_2, ...;

Examples of variable declaration statements:

int index;
int i, j, k;
long number_of_seconds_in_a_year;
real x, y, z, pi;
double d;
bool b, is_ok; 

Once a variable name is declared, it will be associated with an area in memory of different size for different variable type. To store a value to that area in memory, we need to use a assignment statement, with the following syntax:

assignment_statement: 
   variable_name = literal;

Rule: The assignment statement takes the value on the right hand side of the '=' sign, and stores it directly into the area in memory reserved for the variable name on the left hand side of the '=' sign, if the value and variable are the same type, and size of the variable is big enough to hold the value.

Based on this rule, we can not assign a boolean literal to an integer variable. If we do, the C# compiler will give us a type mis-match error.

Also based on this rule, we can not assign an integer value that is very big, for example 9876543210, to an int type of variable. If we do, the compiler will also give us an error.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

Data Type and Variables

 Data Literals

 Data Literals - Example

Variables and Assignment Statements

 Variables and Assignments - Example

 Arithmetic Operations

 Arithmetic Operations - Example

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version