What are Data Types in C#?

When coding in C#, data types and variables are important concepts you must understand. Understanding their characteristics makes it easier to discern the appropriate instances where you can use them without your program yielding unsavory results. When it comes to variables, you need to know the unique features and how to declare each variable correctly in your code. Other than that, you must also know how to assign values to variables.

In programming, you will often use random values whose properties can change while in execution. You can, for example, create a program that measures the body mass index (BMI). Obviously, since the calculations depend on the individual’s mass and height, you can expect different entries from each user. Therefore, when writing the program, you do not know the values that each user will introduce at the input, so the best option is to write a program that can process all possible values users can enter.

Each time someone enters either the mass or height values to determine their BMI, they are temporarily stored in the computer’s RAM. It is such values that change at execution depending on the user’s input that we collectively refer to as variables.

Data Types

Data types refer to a range of values that share similar characteristics. For example, the byte data type refers to integers within the range of 0 and 255. Data types are identified according to their names, size (memory allocation).

and default values. In C#, the basic data types are also known as primitive data types because they are built-in. They are hard-coded into the C# language at the fundamental levels. Let’s look at the primitive data types below:

Integer Types

These types refer to integer values and are as follows:

  • sbyte

These are signed 8-bit integers, meaning that they can only handle up to 256 values (2^8), including negative and positive values. From the default value o, the minimum and maximum values stored by sbyte can be expressed as follows:

SByte.MinValue = -128 (-27)
SByte.MaxValue = 127 (27-1)

  • byte

Unlike sbyte, these are unsigned 8-bit integers. They can also handle up to 256 integer values. However, bytes cannot be negative values. The default byte value is o, and the minimum and maximum values stored in bytes can be expressed as follows:

Byte.MinValue = o
Byte.MaxValue = 255 (2^8-1)

  • short

These are signed 16-bit integer values. The default value for the short type is o, while the minimum and maximum values stored can be expressed as follows:

Int16.MinValUe = -32768 (-2^15)
Inti6.MaxValue = 32767 (2^15-1)

  • ushort

These are unsigned 16-bit integer values. The default value for ushort is o, while the minimum and maximum values stored can be expressed as follows:

UInti6.MinValue = o
UInti6.MaxValue = 65535 (2^16-1)

  • int

These are 32-bit signed integer values. By now you can see that as the bits grow, the number of possible values that can be stored in the data type also grows. The default value for int is o, while the minimum and maximum values stored can be expressed as follows:

Int32.MinValue = -2,147,483,648 (-2^31)
Int32.MaxValue = 2,147,483,647 (23^1-1)

Of all the data types, int is the most commonly used in most programming languages. This is because it naturally fits a 32-bit microprocessor and the fact that it is large enough to handle most of the calculations performed in normal life.

  • uint

These are 32-bit unsigned integer values. The default value for uint is ou, which can also be written as ou. This is one of the few exceptions in C# programming where upper or lower case mean the same thing. When using this data type, you must be careful to include the letter U, otherwise, the compiler will interpret it as an int data type. The minimum and maximum values stored can be expressed as follows:

UInt32.MinValue = o
UInt32.MaxValue= 4,294,967,295(2^32-1)

  • long

These are 64-bit signed integer types whose default value is ol, which can also be written as °L. When using this data type, it is often advisable to use the upper case L because the alternative can be misconstrued for 1. Another common mistake people make is to forget the L, in which case the compiler interprets the values like int, yet we know the number of values held within cannot match.

The minimum and maximum values stored can be expressed as follows:

LInt64.MinValue = -9,223,372,036,854,775,808 (-263)
LInt64.MaxValue = 9,223,372,036,854,775,807 (263-1)

  • ulong

This is by far the largest integer type in C# programming. The 64-bit unsigned type has the default value as ou or on. Without the U, compilers will interpret the data type as long values, so you must also be careful with that.

The minimum and maximum values stored can be expressed as follows:

UInt64.MinValue = o
UInt64.MaxValue 18,446,744,073,709,551,615 (264-1)

Let’s use an example to explain how you can declare several variables of the integer data types mentioned above.

// To declare loan repayment variables

byte years = 2o;

ushort weeks = 1042;

uint days = 7300;

ulong hours = 175200;

// Print output to console

Console.WriteLine(years + ” years are ” + weeks + ” weeks, or ” + days + ” days, or ” + hours + ” hours.”);

// output:

// 20 years are 1042 weeks, or 7300 days, or 175200 hours.

ulong maxIntValue = UInt64.MaxValue;

Console.WriteLine(mwdntValue); // 18446744073709551615

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.