In this video
will try to understand
what is the importance of bin folder
and obj folder
in C# project
and C# compile.
In both of these folders we have compiled code.
When we write any code in C# language and hit compile then both of these folders have
the DLLs and the compiled code.
In the bin folder and in obj folder also we have compiled code.
We have only one compiled code then why do we need two folders not just one folder. In that folder why don't we have the compiled code?
We have
two folders
because
the compilation process goes through two steps
Compiling and Linking.
Above we have a diagram where it explain
the process of compiling is divided into two phases.
In compiling
every code file is compiled into individual compile units.
If we have project with multiple files like File1.cs and File2.cs.
Each of the file is compiled into individual units.
The second process is
these compile code files which are compiled individually is later linked into a single assembly
which can be a dll and an exe depending on what project type we have selected.
In the compiled phase
first, every code file of a project is compiled individually
then in the link phase
all of these compiled code files are combined to create a single unit which can be a dll or can be an exe.
Whatever happens in the compile
phase the individual compile units
actually goes in the obj folder.
after the link phase the final code or compilation
goes into the bin folder.
If we see both of these folders
this is the bin folder output
At the left hand side we have kept the obj folder output
and at the right hand side the bin folder is flashed.
There are many files in obj folder as Temporary generated files
because for every code file which we have in the project, it is creating the individual compile units.
At the right hand side it has created only one exe and one dll.
Basically, intermediate compilation which happens wherein
first individual files are compiled to individual compiled units actually go into the obj folder.
Later on after the linkage it is either one exe either one dll.
There are many files in the obj folder but there are less number of files in the bin folder.
Still
this does not answer the question
why do we need two folders?
We can just have one folder in the project i.e. the bin folder
and these intermediate compilation can happen in some temp directory.
where this intermediate compilation happens.
In the project folder we just have the bin folder i.e the final folder.
At this moment this obj folder is also a part of the project.
This is ConsoleApp3 on Visual Studio
inside this there is an obj folder.
What is the point of having the obj folder inside the project?
whenever we backup this project
whenever we check in this project
the obj folder also becomes a part of the checking and backing up.
one of the biggest achievement or the use of having this folder is
Conditional Compilation or Incremental Compilation.
Let's assume we have a project and in the project we have lot of solutions & projects. In that project we have multiple files like File1.cs, File2.cs, File3.cs, File4.cs and so on.
First, will compile it and intially when we compile it will compile everything and create the final dll or exe in the in the bin folder.
Afterwards if we make changes in File3.cs
if only File3 has changed then only File3 should compile.
That information is only there in the obj folder.
In the obj folder we have individual entries of the file compilation so this is the only folder where we can check the date & time
and if only File3 has changed then only File3 should compile.
Because of the obj folder we can have conditional compilation & incremental compilation that means it only compiles those files which have changed
that will make the compilation faster.
Will see a demo
of conditional compilation how does it work in C#.
We have the solution which has ClassLibrary1, ConsoleApp3 and so on.
We have the bin folder and obj folder.
First delete both of these folders
If we build the solution here it will build class library it will also build the ConsoleApp3 as it does not have any history of what has been built and not has been built.
Now if we do a build again
it will create a bin folder and obj folder.
In the obj folder it has created individual entries of every file.
Now if we do a build without changing anything
it does not build any files.
If we change something
it will compare with the obj folder saying is this date & time changed or something.
If we do a build it did not compiled ClassLibrary3,
has only compiled ConsoleApp3 - Incremental & Conditional compilation.
It has only compiled what we changed.
That is happening because of obj directory.
If we again delete it
and do build here
It has not compiled ClassLibrary because even the class library has its own obj folder.
Delete ClassLibrary obj folder
and do a build
it will compile both of them.
By having the obj folder we get incremental compilation, conditional compilation and compilation process is faster.
If we have small project or sample application practicing we can delete the obj folder
In big projects keep this obj folder, it will save you time.
In this video we were trying to understand what is the difference between the bin folder and the obj folder in C# projects.
Thank you very much!