cft

Data-types in C#

Understanding Data types in C#.


user

Ubong Umoh

2 years ago | 2 min read

So what are data-types? Data-types are what specifically tells the type of data a variables that has been declared can store or hold… We also know C# is a strongly typed language and hence we need to declare the type either implicitly or explicitly while creating a variable, so how are variables declared in c# I know that's your next question😃

Variables are declared in c# in this way int A= 12 or string Name="Ubong" i.e the first statement int tells us that the variable A can only contains integers while the variable Name can only contains strings.

There are two types of Data-Types in c# which are:

  • value type
  • Reference Type

Value Type : These types holds the actual data that are assigned to the variable in the stack Memory because they aren't complex, it holds a data value within its own memory space i.e

int s = 12;

string car= "Volvo";

so what happens behind the scene is that the
variable is created in the stack with it's data
type and also the value assigned to the variable..

Reference Type : The reference type are the opposite of the values type, they don't store the actual value but rather a memory location that points to where the actual data are being allocated in the heap memory because they are used for dynamic storage and complex type… i.e

Public Class Student{
public int id;
public string Name;
public string Grade;
}
Student s = new Student();

The above statement is a reference type declaration which is of type student, what we did above is we created a reference type and stored it in the variable named s, the new keyword assigned to the declared variable s creates a memory allocation in the heap and assigns the memory address to
s, in this case s doesn't hold the actual data but rather the memory address………
To access the properties in Student class we do the following
Student s = new Student();
s.Id=1;
s.Name="Ubong";
s.Grade="Good"
or
it can also be accessed by doing the following
Student s = new Student(){
s.Id=1,
s.Name="Ubong",
s.Grade="Good"
};

So what are the differences between Value and Reference Types ?

  • 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 your 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
var z = 5;
The compiler will automatically detects it's an integer since we assigned a numeral.

I hope you enjoyed the article, Thanks for going through this article♥️….

Upvote


user
Created by

Ubong Umoh

I'm a dotnet developer mainly focused on Web, API development and also write tech related articles


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles