Data types

Intro

Each piece of data has it's own data type, because the type of a piece of data determines what opertations can be done with it. Some operations on certain data does not make sense and will result in an error.

int num = 23;
string text = "sometext";
var result = num * text; // this operation makes no sense

For a computer everything is one and zero in the end.

1001110111011101

The computer has to know where the data starts and where it ends to interpret it correctly.

Picking the right data type

If you want to light a candle on a cake, do you use a flamethrower to light the cake?

If you want to store numbers between 1 and 100000 you could use a uint, int, short, ushort, float, double, etc.
It is your job to determine the correct data type for all the data used in a program.

We do not have to know all types, use the documentation for this.

Reference vs. Value types

Variables of reference types store references to the data. Value types directly contain the data.

In ohter languages, like Java described as primitive data types.

Examples of value types are:
  • int, double, short, ... all numeric types
  • char, bool
  • enum
  • struct
Examples of reference types are:
  • string
  • object
  • interface
  • delegate

For a full list and more details please visit the documentation: Types and variables

object data type

In C# all types, built-in and user-defined inherit from Object. You can assign any type to variable of the type object.

Enum data type

The enum keyword is used to declare an enum type. Enums are used to give a written name to a constant value.

As classic example: think of the seven days of a week. We could store those days as values 0 to 6. An enum alows us to store the names of the weekdays as constant numeric values.

Declaring an enum:
enum weekdays {
    monday, tuesday, wednesday, thursday,
    friday, saturday, sunday
}
Using an enum

To use an enum, please remember that this is a datatype. Just like when using a string data type, we usually store those within variables.

weekdays day; // Declaring a variable
day = weekdays.tuesday; // Storing a value
Example:
weekdays day01 = weekdays.tuesday; // Storing a value
weekdays day02 = weekdays.friday; // Storing a value
if (day01 < day02) {
    Console.WriteLine(day01 + " is before " + day02);
}
Ouput:
tuesday is before friday

Autoincrement

If we would like to use the numbers from 1 to 7 for the weekdays we can define the first element as 1 and all the following elements are going to be each incremented by one.

enum weekdays {
    monday = 1, tuesday, wednesday, thursday,
    friday, saturday, sunday
}

Casting datatypes

Console.WriteLine(weekdays.tuesday);
tuesday

The above code prints the written name from the enum to the console.

If we would like to use the numerical value we have to use a cast operation:

Console.WriteLine( (int)weekdays.tuesday );
2

..with regard to the autoincrement example

Implicit vs. explicit type conversion

Consider the following example:

Console.WriteLine( 'a' + 5 );

Output:

102

The number is a logic result of the real representation of symbols.

American Standard Code for Information Interchange ASCII Wikipedia

In chapter 4 Strings this is also mentioned.

Let's say we would like to see the character of the ASCII code instead of the numerical value, we can use the cast expression to convert it back.

Console.WriteLine( 'a' + 5 ); // Implicit Conversion To Integer Prints 102
Console.WriteLine( (char)('a' + 5) ); // Explicit Conversion To Char Prints f

So if a conversion happens, without us telling the programm to do so, we call it an implicit conversion. If we explicitly tell the program to convert a type this is an explicit conversion.