Language: EN

csharp-tipos-de-datos

What are and how to use types in C#

C# is a strongly typed, object-oriented programming language. Therefore, data types determine the characteristics and behavior of the variables that are manipulated throughout the execution of a program.

In C#, types are divided into two main categories:

  • Value types, which contain their data directly
  • Reference types, which contain a reference to their data

Value Types

C# provides a series of predefined value types that correspond to the primitive types of the .NET Framework. Among the most common are the following:

Integers

The integer data types in C# are used to represent whole numbers (without a decimal part). Some of the most common integer data types are:

TypeBytesSigned/UnsignedMinimum ValueMaximum Value
byte1Unsigned0255
sbyte1Signed-128127
short2Signed-32,76832,767
ushort2Unsigned065,535
int4Signed-2,147,483,6482,147,483,647
uint4Unsigned04,294,967,295
long8Signed-9,223,372,036,854,775,8089,223,372,036,854,775,807
ulong8Unsigned018,446,744,073,709,551,615

Examples,

int number = 10;
long longNumber = 100000L;  // L is necessary to indicate it's of type long
short shortNumber = 30000;

Floating-Point Numbers

The floating-point data types in C# are used to represent numbers with decimal parts. Some of the most common floating-point data types are:

TypeBytesPrecisionMinimum ValueMaximum Value
float4Single precision±1.5 x 10^-45±3.4 x 10^38
double8Double precision±5.0 x 10^-324±1.7 x 10^308
decimal16High precision±1.0 x 10^-28±7.9 x 10^28

Examples,

float floatNumber = 10.5f; // f is necessary to indicate it's of type float
double doubleNumber = 20.99;
decimal decimalNumber = 100.50m; // m is necessary to indicate it's of type decimal

Boolean Data Types

The boolean data type in C# is used to represent values of true or false. They are necessary when we need to evaluate conditionals.

bool isTrue = true;
bool isFalse = false;

Character Data Types

The character data type in C# is used to represent a single character (a letter). The char data type is used to store Unicode characters.

For example, it can be used to store letters, numbers, or special characters.

char letter = 'A';

In C#, characters are delimited with single quotes '.

Structures

struct allows the creation of user-defined value grouping types.

public struct Point
{
    public int X;
    public int Y;
}

Point point = new Point();
point.X = 10;
point.Y = 20;

Enumerations

enum allows the creation of a set of related constants under a specific type name.

enum Days
{
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

Days today = Days.Monday;

Reference Types

Reference types include string, arrays, classes (class), and delegates. For example, string is a class provided by C# to work with text strings.

Example of using a reference variable, in this case a string.

int[] numbers = {1, 2, 3, 4, 5};
string greeting = "Hello, World!";

We will look at these types in more detail in later entries