This section shows you the list of all the available built in data types in C# programming language. The C# program made up of many variables. Variable is nothing but a name given to the memory location.
In a memory location, we keep changing the value, so what type of data we want to store in that location is nothing but a data type. Based on the C# data type, the allocated memory size to the variable, and the memory’s format decided.
In many cases, the size may be the same, and the format may vary. For example: In C# language, the size of Long and Float built in type is 4. However, the format in which the data stored in the memory location is different.
C# Built in Data Types
The following are the available list of built-in data types in C# Programming language.
C# Boolean Data Type: The Boolean Data Type is used in the case when we want to store the value true or false.
It accepts only true or false. For example,
static void Main() { bool b = true; }
If we want to ask a user, “Do you live in the United States?” then the answer should be either true or false. In such cases, we use the C# Boolean Data Type.
C# Integral types: The Data Types which doesn’t have any decimal part is said to be integral types.
There are several integral Data Types like sbyte, byte, short, ushort, int, uint, long, ulong, char. All these integral Data Types are capable of holding some numbers.
Type | Range | Size |
---|---|---|
sbyte | -128 to 127 | Signed 8-bit integer |
byte | 0 to 255 | Unsigned 8-bit integer |
char | U+0000 to U+ffff | Unicode 16-bit character |
short | -32768 to 32,767 | Signed 16-bit integer |
ushort | 0 to 65,535 | Unsigned 16-bit integer |
int | -2,147,483,648 to 2,147,483,647 | Signed 32-bit integer |
uint | 0 to 4,294,967,295 | Unsigned 32-bit integer |
long | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | Signed 64-bit integer |
ulong | 0 to 18,446,744,073,709,551,615 | Unsigned 64-bit integer |
byte: A variable of byte Data Type can hold a number between 0 and 255(256 values), i.e., 8-bit integer.
The C# built-in byte data type contains 8 bits, and each bit represents either 0 or 1. Now 8 bits together represent 28 distinct values. For example, if we want to store the value for the ‘age of a person’, type byte is more than enough.
sbyte: Here, ‘s’ stands for signed, i.e., both negative and positive values. As shown in the above table, it can hold a number from -128 to 127, which means 28 (256) values.
Int: A variable of type integer can hold both positive and negative values. We have already shown its range in the given in the table. It can hold a 32-bit integer.
For example, if we are storing the population of any country, probably we would go for int because it can have a big number.
The population can be a definite positive number so that we can choose uint or if it is even in a big range, we can go for long or ulong.
Depending on the requirement of the application, we can choose different types. But practically, it is not possible to remember the minimum and maximum values each Data Type can hold.
In case if you have MSDN accessible, it is fine, but in case if it is not, you can even know from visual studio.
We are here showing all the properties of the type int.
Let me show you an example C# code of built-in data types.
using System; class Program { static void Main() { Console.WriteLine("Min = {0}", int.MinValue); Console.WriteLine("Max = {0}", int.MaxValue); } }
OUTPUT
C# Floating point types
Float and Double are the two floating-point built-in data types in C#.
To store precision (digits after the decimal point) values, we choose either float or double types.
Type | Approximate range | Precision |
---|---|---|
Float | ±1.5e-45 to ±3.4e38 | 7 digits |
Double | ±5.0e-324 to ±1.7e308 | 15-16 digits |
Data Type Float can store 32-bit floating point values, whereas double can store 64-bit floating point values.
C# Fixed point type
Decimal is a fixed point type that holds a fixed precision of 28 digits. It indicates a 128-bit Data Type. For finding any financial calculations, or any scientific measurements, we chose decimal type decimal. It is because the decimal type will give the result, which is very closer to its actual value.
For example, 10/3 is not 3. The actual value will be something like 3.333.