top of page

C# data types

Almost every C# test I've taken has a lot of questions about data types and casting. Hopefully this page will help answer some of the questions.

Common data types

​

int      - Stores whole numbers from -2,147,483,648 to 2,147,483,648   int myNum = 9999;

long   - Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Used if int is not large enough

float   - Stores fractional numbers. Recommended for storing 6 to 7 decimal digits 

double - Stores fractional numbers. Sufficient for storing 15 decimal digits. 2nd most used number data type. Often used for money.

bool     - Stores true or false. Common used for conditional testing, like loops.

char     - Stores a single character/letter, surrounded by single quotes

string   - Stores a sequence of characters, surrounded by double quotes. Probably the most common used data type.

C# Type Casting 

​

Two types of type casting

  1. Implicit casting (automatically) - converting a smaller type to a larger data type 

   char - int - long - float - double​

  1. Explicit casting (manually) - converting a larger type to a smaller type

           double - float - long - int - char​

​

For example, let's say we have an int as

int myInt = 9;

double myDouble = myInt;

In this case, since int is higher up the implicit food chain, myDouble is equal to 9.

​

Another example, let's say we have a double variable

double myDouble = 9.78;

int myInt = (int) myDouble;

In this case, since int is lower on the explicit food chain, myInt is equal to 9.

​

Confusing, yes it can be!

bottom of page