Table of Contents
Introduction to Data Types in C# Programming Language: Overview and Examples
Integer
Integer data types are used to work with numbers. In this case, the numbers are whole numbers like 10, 20 or 30. In C#, the datatype is denoted by the Int32 keyword. Below is an example of how this datatype can be used. In our example, we will define an Int32 variable called num. We will then assign an Integer value to the variable and then display it accordingly.
| Data Type | Size (bytes) | Range | Description |
| sbyte | 1 | -128 to 127 | Signed 8-bit integer |
| byte | 1 | 0 to 255 | Unsigned 8-bit integer |
| short | 2 | -32,768 to 32,767 | Signed 16-bit integer |
| ushort | 2 | 0 to 65,535 | Unsigned 16-bit integer |
| int | 4 | -2,147,483,648 to 2,147,483,647 | Signed 32-bit integer |
| uint | 4 | 0 to 4,294,967,295 | Unsigned 32-bit integer |
| long | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | Signed 64-bit integer |
| ulong | 8 | 0 to 18,446,744,073,709,551,615 | Unsigned 64-bit integer |
Example
Here’s an example program in C# that demonstrates all the data types you've mentioned in one program:
using System;
class Program
{
static void Main()
{
// 8-bit unsigned byte: Range 0 to 255
byte byteValue = 255;
Console.WriteLine($"byte: {byteValue}");
// 8-bit signed sbyte: Range -128 to 127
sbyte sbyteValue = -128;
Console.WriteLine($"sbyte: {sbyteValue}");
// 16-bit signed short: Range -32,768 to 32,767
short shortValue = -32768;
Console.WriteLine($"short: {shortValue}");
// 16-bit unsigned ushort: Range 0 to 65,535
ushort ushortValue = 65535;
Console.WriteLine($"ushort: {ushortValue}");
// 32-bit signed int: Range -2,147,483,648 to 2,147,483,647
int intValue = -2147483648;
Console.WriteLine($"int: {intValue}");
// 32-bit unsigned uint: Range 0 to 4,294,967,295
uint uintValue = 4294967295;
Console.WriteLine($"uint: {uintValue}");
// 64-bit signed long: Range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
long longValue = -9223372036854775808;
Console.WriteLine($"long: {longValue}");
// 64-bit unsigned ulong: Range 0 to 18,446,744,073,709,551,615
ulong ulongValue = 18446744073709551615;
Console.WriteLine($"ulong: {ulongValue}");
}
}
Output:
byte: 255
sbyte: -128
short: -32768
ushort: 65535
int: -2147483648
uint: 4294967295
long: -9223372036854775808
ulong: 18446744073709551615
This program illustrates how each data type is declared and used with its respective range of values.
Explanation of the Program:
- byte: Holds values from 0 to 255.
- sbyte: Holds signed values from -128 to 127.
- short: A signed 16-bit integer.
- ushort: An unsigned 16-bit integer.
- int: A signed 32-bit integer.
- uint: An unsigned 32-bit integer.
- long: A signed 64-bit integer.
- ulong: An unsigned 64-bit integer.