Language: EN

csharp-tipos-de-datos

What are and how to use types in C#

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

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

  • Value types, directly contain their data and are stored on the memory stack
  • Reference types, contain a reference to their data, which is stored on the heap

Value types

C# offers a series of predefined value types that correspond to the primitive types in the .NET Framework. Some of the most common integer data types are:

Integers

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

Example:

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

Floating-point numbers

Floating-point data types in C# are used to represent numbers with a decimal part. 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 is a float type
double doubleNumber = 20.99;
decimal decimalNumber = 100.50m; // m is necessary to indicate it is a decimal type

Boolean data types

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

Example:

bool isTrue = true;
bool isFalse = false;

Character data types

The character data type in C# is used to represent a single character. The char data type is used to store Unicode characters. For example, it can be used to store letters, numbers, or special characters.

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

char letter = 'A';

Structures

structs allow 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

enums allow the creation of a set of related constants under a specific type name.

Example:

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, the string type is a class provided by C# for working 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 future entries.