What is a variable?

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

What is a data type?

The contents within a variable can have different data types. The data type declares the type of information contained in 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.

Variable manipulation

Variable manipulation is the process of creating variables, assigning values, and updating those values. Let’s learn variable manipulation in C#.

How to declare a variable?

In the example below, a variable of the string data type is declared with a variable name of myStringName. Declaring a variable is like creating an empty, named box.

string myStringName;

How to assign a value to a variable?

string myStringName = "Hello";

The variable myStringName, now contains the Hello value within it.

myStringName = "Hello again";

In the example above, the value is changed to Hello again but the data type isn’t declared again.

Press + to interact
string myStringName= "Hello"; // declaring and assigning value
Console.WriteLine(myStringName);
myStringName= "Hello again"; // reassigning a new value
Console.WriteLine(myStringName);

Examples of numerical data types

Let’s look at the following examples of numerical data types:

Press + to interact
int numOne = 5;
Console.WriteLine(numOne);
float numTwo = 2.55f; // When declaring a float, an "f" needs to be after the value
Console.WriteLine(numTwo);
double numThree = 3.33;
Console.WriteLine(numThree);
decimal numFour = 4.66m; // When declaring a decimal, an "m" needs to be after the value
Console.WriteLine(numFour);

What is the var data type?

To avoid confusion, it’s best to be specific when declaring data types. However, it’s also possible to use the var data type and the compiler will then attempt to determine which data type to use.

Press + to interact
var theWord = "hello" ; // Declares a string and assigns value "hello"
Console.WriteLine(theWord);
var theNumber = 5; // Declares an int and assigns value 5
Console.WriteLine(theNumber);

The data type of a var variable can’t be changed later once a value is assigned to it. In the example above, we can’t assign an int value to theWord variable. It accepts only string values after line 1.

Allocated memory and ranges of C# data types

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 table 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 Type

Bytes

Range

Example

byte

1

0–255

7

short

2

-32,768–32,767

-10

int

4

-2,147,483,648–2,147,483,647

12

long

8

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

-54

sbyte

1

-128–127

5

ushort

2

0–65,535

7

uint

4

0–4,294,967,295

9

ulong

8

0–8,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, but 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. If negative numbers are not included, however, then the positive numbers double when making it unsigned.

Using the unsigned data type for short (which is u short), the range becomes 0 to 65,535.