What are datatypes? C# being a strongly typed language requires the type of the variable to be explicitly or implicitly defined, so this brings us to the topic data-types...
Data-types are used when declaring a variable to tell the type of values that such variable can hold i.e ``` int a = 5 this tells us that the variable named a can only hold values that are integers while string s = "Ubong" tells us that the variable named s can only hold strings or collection of characters.
We have two major types of data-types in c# which are the value type and Reference types, so what are the differences between these two types ?Hmmm
- Values Types : These types stores the actual data that is assigned to a variable, but the data types including the variable name and the data are stored in the **stack**
i.e int x = 10 or
string s =" Sky"....... .
```so we can have something like this Console.Writeline($"{s} age is {x}"); which prints out Sky age is 10...
- Reference Types : These types stores the reference to the actual data, they don't store the actual data but rather the address where the actual data is stored they are majorly used with user defined types in c# i.e
Public Class Person
{
public int Id;
public String Name;
}
Person p1 by doing this we are declaring a reference type variable p1 but this will be assigned as null since no memory location have been created on the heap for allocation but by doing the following below we are creating a memory allocation and assigning it to p
Person p1 = new Person();
we can now assign data or values to the object properties by doing the following
p1.Id=1;
p1.Name="Umoh" or by using object initializer
Person p1 = new Person(){
p1.Id=1,
p1.Name="Umoh"
};
So what are the differences between the two
Value types stores the actual data while the reference types hold the memory address to the actual data.
Value types data are stored in the stack while reference types data are stored in the heap and also Heap are mainly for dynamic memory storage i.e memory that grows and for complex data-types
Lemme guess you next question, what if we aren't sure of the type of data we wanna store.......
There is a solution worry no more, so there is a new keyword called the var it is used to implicitly declare variables and the compiler automatically detects the data-type based on the values assigned to the variable, yuppi I got you there right i.e
var z = 5; The compiler will automatically detects it's an integer since we assigned a numeral