C# Fundamentals - Third Edition

A variable is a named location in computer memory that holds information for later use. The information within the variable can change or can be used when needed.  Think of a variable as a box that has been named; the contents within the box can change, but the box name stays the same.

The contents within a variable can have different data types. The data type declares what type of information is going to be contained within the variable. There are many kinds of data types, such as a string for text (“Hello World”) or an integer for numbers (54) with which mathematical calculations can be performed.

In the example below, a variable of data type string is declared with a variable name of “myStringName”. Declaring a variable can be thought of as creating an empty box that is named.

Example: Declare a variable

string myStringName;

 

Example: Declare a variable and assign a value

string myStringName = "Hello";

The variable “myStringName” now contains the value “Hello” within it. In the example below, the value is changed to “Hello again” and the data type is not declared again when changing the value. Once the data type is declared, it is not declared again.

Example: Assign a new value

myStringName = "Hello again";

 

Example: Declare a variable and assign an integer value

int myIntName = 2 + 3; // myIntName is now 5

A string puts the two values together; an integer adds them together.

When declaring certain numeric data types, such as a float or a decimal, it is required to put a character behind the value. The example below demonstrates this.

Example: Numeric data types

int numOne      = 5;
float numTwo    = 2.55f; // When declaring a float an "f" needs to be after the value
double numThree = 3.33;
decimal numFour = 4.66m; // When declaring a decimal an "m" needs to be after the value

 

It is recommended to be specific when declaring data types to avoid confusion. However, it is possible to use the data type var and the compiler will attempt to figure out which data type to use.

Example: var

var theWord = "hello"; // Declares a string and assigns value "hello"
var theNumber = 5; // Declares an int and assigns value 5

 

Every data type uses a specific amount of memory when declared. The smallest measurement is a bit, and eight bits make up a byte. The chart below lists a few of the most common data types along with how many bytes they use, their range, and an example of their data.

Data Types

Bytes

Range

Example

byte

1

0 to 255

7

short

2

-32,768 to 32,767

-10

int

4

-2,147,483,649 to 2,147,483,647

12

long

8

-9,233,372,036,854,775,808 to 9,233,373,036,854,775,807

-54

sbyte

1

-128 to 127

5

ushort

2

0 to 65,535

7

uint

4

0 to 4,294,967,295

9

ulong

8

0 to 18,446,744,073,709,551,615

11

float

4

Represents a floating point value up to 7 digits

12.3

double

8

Represents a floating point value up to 16 digits

12.35

decimal

16

Represents a floating point value up to 29 significant digits

12.356

bool

1

Logical Boolean type (can only be True or False)

True

char

2

A single Unicode character

H

string

varies

A sequence of characters

Hello

 

Integral types can be signed types or unsigned types. A signed type value can be positive or negative and unsigned types can only be positive values. For example, a short, which is a signed type, can have a value from -32,768 to 32,767. However, if negative numbers are not included, then the positive numbers will double when making it unsigned. Using the unsigned data type for short (which is ushort), the range will become 0 to 65,535.