News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

C# Tutorial – 04 – Variables

Variables are used for storing data during program execution.

Data types

Depending on what data you need to store there are several different kinds of data types. The simple types in C# consist of four signed integer types and four unsigned, three floating-point types as well as char and bool.

Data TypeSize (bits)Description
sbyte
short
int
long
8
16
32
64
Signed integers
byte
ushort
uint
ulong
8
16
32
64
Unsigned integers
float
double
decimal
32
64
128
Floating-point numbers
char16Unicode character
bool4Boolean value

Declaration

In C#, a variable must be declared (created) before it can be used. To declare a variable you start with the data type you want it to hold followed by a variable name. The name can be almost anything you want, but it is a good idea to give your variables names that are closely related to the value they will hold.

int myInt;

Assignment

A value is assigned to the variable by using the equals sign, which is the assignment operator (=). The variable then becomes defined or initialized.

myInt = 10;

The declaration and assignment can be combined into a single statement.

int myInt = 10;

If multiple variables of the same type are needed there is a shorthand way of declaring or defining them by using the comma operator (,).

int myInt = 10, myInt2 = 20, myInt3;

Once a variable has been defined (declared and assigned) it can be used by referencing the variable’s name.

System.Console.Write(myInt); // "10"

Integer types

There are four signed integer types that can be used depending on how large a number you need the variable to hold.

// Signed integers
sbyte myInt8  = 2;  // -128   to +127
short myInt16 = 1;  // -32768 to +32767
int   myInt32 = 0;  // -2^31  to +2^31-1
long  myInt64 =-1;  // -2^63  to +2^63-1

The unsigned types can be used if you only need to store positive values.

// Unsigned integers
byte   uInt8  = 0;  // 0 to 255
ushort uInt16 = 1;  // 0 to 65535
uint   uInt32 = 2;  // 0 to 2^32-1
ulong  uInt64 = 3;  // 0 to 2^64-1

In addition to the standard decimal notation, integers can also be assigned using hexadecimal notation.

int myHex = 0xF; // hexadecimal (base 16)

Floating-point types

The floating-point types can store real numbers with different levels of precision. Constant floating-point numbers in C# are always kept as doubles, so in order to assign such a number to a float variable an “F” character needs to be appended to convert the number to the float type. The same applies to the “M” character for decimals.

float   myFloat   = 3.14F; // 7 digits of precision
double  myDouble  = 3.14;  // 15-16 digits of precision
decimal myDecimal = 3.14M; // 28-29 digits of precision

A more common and useful way to convert between data types is to use an explicit cast. An explicit cast is performed by placing the desired data type in parentheses before the variable or constant that is to be converted. This will convert the value to the specified type, in this case float, before the assignment occurs.

myFloat = (float)myDecimal; // explicit cast

The precisions shown above refer to the total number of digits that the types can hold. For example, when attempting to assign more than 7 digits to a float, the least significant ones will get rounded off.

myFloat = 12345.6789F; // rounded to 12345.68

Floating-point numbers can be assigned using either decimal or exponential notation.

myDouble = 3e2; // 3*10^2 = 300

Char type

The char type can contain a single Unicode character delimited by single quotes.

char c = '3'; // Unicode char

Bool type

The bool type can store a Boolean value, which is a value that can only be either true or false. These values are specified with the true and false keywords.

bool b = true; // bool value

Variable scope

The scope of a variable refers to the code block within which it is possible to use that variable without qualification. For example, a local variable is a variable declared within a method. Such a variable will only be available within that method’s code block, after it has been declared. Once the scope of the method ends, the local variable will be destroyed.

static void Main()
{ 
  int localVar; // local variable
}

In addition to local variables, C# has field and parameter type variables, which will be looked at in later chapters. However, C# does not have global variables, as for example does C++.