Learn about Basic Data Types of C#

In C#, the most used data types are int, bool, double, string, and Datetime.

But if we look closely into the Data types that are available in C#, there are Value Data Types, Reference Data Types, and Pointer Data Type

What is Value Data Type?

We call it Value Data Type when the data type stores the value directly in the memory.

For example

int, boolean, and chars are examples of value data types.

When you check the definition of this type, it is defined with struct, and struct is one that defines value data types.

There are 2 different kinds of Value Data Type

  1. Predefined Value Data Types are ones we use every day like Integer, Boolean, Long, Double, etc.
  2. User Defined Value Data Types are Structure and Enumerations. It is a data type that is derived from an existing Data type.

What is the Reference Data Type?

It is data that is stored by the reference to a variable. It does not store actual data value, it stores the reference to data.

There are 2 kinds of Reference Data Types too

  1. Predefined types are String, Object, etc, which are already there referring to not storing values.
  2. User-defined types are Classes and interfaces that are created by the developer and save the reference, not the value.

What is the Pointer type?

A pointer is also known as a locator that points to the address of the value. The pointer type variable stores the memory address of the type and uses special characters.

  • ampersand(&) it is the address operator
  • artesrisk(*) it is Indirection operator
    // declare a variable
    int numb = 1234;
    
    // its how to declare pointer type
    int* ptr = &numb;
    
    // this prints out the numb values
    Console.WriteLine($"Value :{numb}");
    
    //this will print out address of ptr, and ptr typecasted with int to be printed cause its pointer type
    Console.WriteLine($"Address :{(int)ptr}");

Output

Value

These are the brief idea of types in C#.