C# data types

Before reading this section, we’re expecting you to be familiar with variables. If don’t then please make sure you read the C# variables section first.

So far we know that every time we create a variable, portion of the memory space will be set aside for that variable. But the question is how large this memory space will be and what can we store in that variable?

This is where data types come to the rescue.

We know that first we set the data type when we want to declare a variable.

There are two reasons why a variable should have a data type:

  1. It helps the compiler to know how much memory space it
    should set aside for that particular variable:

Let’s say a program needs to work with integer values. And also we know that the values the program might need is in a range from 0 to 2000. Armed with this knowledge, we can create a variable that is of type short.

Note: you’ll learn about short data type later in this section but for now just remember that the compiler will set aside enough memory space for a variable of type short to be able to store values range from -32,768 to 32,767.

We could also use a data type like int but this data type takes more memory space and the range of values (0 to 2000) for this particular example is not high enough to make it necessary to use int instead of short.

  1. The compiler and developers know what type of data are
    allowed to be stored in that variable:

As mentioned before, data has different types and that’s why we have multiple data types in C#. Now if there’s a variable in a program, how can developers and the compiler know what type of value should be stored in that specific variable? This is where data types can help.

Data types signal the developers and compilers to see what type of value can be stored in the target variable.

For example because a variable of type int only takes integer values, If we attempt to store float values (like 3.4 or 10.542 etc.) we will get compile time error because we’re trying to store a value of different type compared to the type of the variable.

Now that we have a general view of data-types let’s see how many data types are in C#.

Generally speaking there are two categories of data types supported in C# language:

  1. Custom data types: those data types that are created via
    classes, enums, struct and
    interfaces.

  2. Primitive data types: these types are prebuilt in C# and their
    size and use cases are already declared.

Note: there is also compound data type like array that is covered in later section.

Here we start with the primitive data types. The custom data types are covered in later sections.

The list of basic (primitive) data types supported in C# language:

`int`: A number that does not have fraction part is integer. For example 1, 200, -300 etc. are integer and values like 2.3, 4.4, and 543.023 are not.

When a variable is typed int, the amount of space that will be assigned to this variable is big enough to hold the range of values from -2,147,483,648 to 2,147,483,647.

For example:

int number = 2000000;
int number = 3220;

You might be wondering what will happen if we surpass the limits and assign a value higher or lower than the range of int data-type.

For example:

using System;

namespace quickExample

{
class Program
{
static void Main(string[] args)
{
int number = 2147483648;
Console.WriteLine(number);
}
}
}

Output:

Cannot implicitly convert type ‘uint’ to ‘int’. An explicit conversion exists (are you missing a cast?)

In such case the compiler will simply return error instead of compiling.

Note: a variable of type int will take 4 bytes of memory space.

`short `: This is another data-type that accepts integer values (just like `int`) except the range of the values we can set for the variables of type `short` is smaller compared to `int` data-type.

Specifically, this range is from -32,768 to 32,767.

Note: the memory size of a short variable is 2 bytes.

For example:

short number = 1000;
short number = 30000; `ushort`: the `ushort` data type stands for `Unsigned short`. A variable of this type is not capable of holding negative values. The range of numbers that such variable can hold is from 0 to 65535. ushort number = 65000;

Also just like the `short` type, a variable of type `ushort` takes 2 bytes of memory.

Unsigned integral (`byte`): This is another data-type that accepts integer values (just like `short`) except the range of the values we can set for the variables of type `byte` is smaller compared to `short` data-type.

Specifically, this range is from 0 to 255. This means we can’t put negative values for a variable of type byte.

Note: the memory size of a short variable is 1 byte.

For example:

byte number = 120;
byte number = 250; Singed Integral `sbyte`: A variable of type `sbyte` is capable of holding integer values range from

-128 to 127.

Note: the memory space of such variable is 1 byte.

Example:

sbyte number = 127;
`long`: This data-type also is used for integers numbers (just like `int`) but compared to `int` data-type, a variable that is typed `long` can hold larger range of numbers. More specifically, this range is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Example:

long number = 1000000000;
long number = 2500000000;

Note: the memory space of this data-type is 8 bytes.

`uint`: This data type stands for `unsigned int` and that means no negative value is allowed to be stored in a variable of this type. A variable of this type is also capable of storing integer values range from 0 to 4294967295.

Example:

uint number = 4294967295;

Note: the memory space of a variable of this type is 4 bytes.

`ulong`: this data type stands for `unsigned long` and that means no negative value is allowed to be stored in a variable of this type. Also this data type is used to store integer values that are in the range from 0 to 18446744073709551615.

Example:

ulong number = 18446744073709551615;

Note: the memory space of this data type is 8 bytes.

`char`:

If for any reason in a program we needed to store characters, we can use this data-type. Characters can be letters, numbers, and punctuations. But remember, to handle characters, computers use numerical codes in which certain integer numbers represent certain characters. And for this reason, characters are stored as integers in memory when assigned to a variable that is typed char.

Example:

char character ='A';

In the example above, behind the scene the real stored value in the memory will be the binary code of 65. This is because the integer representation of the character A is 65.

Notes:

  1. Any character that we assign to this type of variables should be
    between two single quotations `’ ‘` like the way example above
    showed.

  2. Unlike the `Java` language, we can’t directly store number
    representation of characters in variables of type char. We need to use
    casting if we want to store the number representation of a
    character into a variable of type char.

Note: casting is explained in later sections.

Example:

using System;

namespace quickExample

{
class Program
{
static void Main(string[] args)
{
char cNumber1 = (char) 65;
char cNumber2 = '1';
Console.WriteLine(cNumber1);
Console.WriteLine(cNumber2);
}
}
}

Output:

A, 1

Note: the memory space for a variable of type `char` is 2 bytes.

`float`: float is a data-type that allows numbers with fraction-part to be stored in the variables of this type. A variable typed `float` has enough memory space that can store values with 8 significant digits.

Example:

float fVariable = 123.45657f;
float fVar = 1234.3521f;
float fVar2 = 1.23456f;

Notes:

  • We should end a value of float type with the character
    `f` or `F`.

  • The memory space of a variable typed `float` is 4
    bytes.

`double`: double is another data-type that allows numbers with fraction-part to be stored in the variables of this type. A variable typed `double` has enough memory space that can store values with 16 significant digits.

Example:

double dVar1 = 1234567890.12345;
double dVar2 = 1.23456789012345;
double dVar3 = 12.3456789012345;

Note: the memory space of a variable typed `double` is 8 bytes.

`decimal`: A variable of type decimal is also capable of holding number values with fraction. A variable type `decimal` has enough memory space that can store values with 30 significant digits.

Example:

using System;

namespace quickExample

{
class Program
{
static void Main(string[] args)
{
decimal dec = 12345.1234567891234567891234567m;
Console.WriteLine(dec);
}
}
}

Output: 12345.1234567891234567891234567

Notes:

  • We need to put either `m` or `M` character at the end of
    values that are assigned to variables of type
    `decimal`.

  • Also the memory space of a decimal data types is 16
    bytes.

`bool`: this data-type stands for Boolean and is used to create a variable that can hold the keywords `true` or `false`.

Example:

using System;

namespace quickExample

{
class Program
{
static void Main(string[] args)
{
bool f = false;
bool t = true;
Console.WriteLine(f);
Console.WriteLine(t);
}
}
}

Output: false true

Boolean values are mostly used in conditional statements like if-statement, while-loop, do-white loop etc.

Note: the memory size of this data-type is 1 byte.

`string`: String data type is used to create variables that can store string characters.

Example:

using System;

namespace quickExample

{
class Program
{
static void Main(string[] args)
{
string name = "John";
string lastName = "Doe";
Console.WriteLine(name);
Console.WriteLine(lastName);
}
}
}

Output: John Doe

Notes:

  • In order to store character string into a variable we use double
    quotation `” “`.

  • String data type is non-primitive data type because it refers to
    an object. That means a String object has methods that can perform
    certain operation.

Note: we will explain the String data type in more details in later section.