Monday, June 11, 2018

bin vs obj folders in C#.

When we compile C# programs you would see 2 folders “bin” and “obj”. In this article we will try to understand the difference and importance of these folders.

Both these folders have compiled IL code, but the question comes is why not just one folder and why two folders?.

We have two folders because the compilation process goes through two steps compiling and linking. See the below diagram.

  • In compiling phase every code file is compiled in to individual compiled units. So if you have two code files two independent compiled codes will be generated.
  • In linking phase all these compiled code files are linked and compiled in to single unit assembly which can be a DLL or EXE.

If you compare both the folders ( see below image) you will find more files in “obj” folder as compared to “bin” folder. We have more files in “obj” folder because it creates seperate compiled code files for each source code file.

So the next question which comes to our mind why do we need compiling in two phase, why not just do it one go. By doing the two phase compiling we achieve incremental or conditional compiling.

When we work with big projects we will have lot of code files and we would like to only compile those code files which have changed. In the “obj” folder we have entry of each code file compilation. So we can know from the same which files exactly have changed , thus making compiling fast.

In summary in “obj” folder have compiled files for each source code file and in “bin” folder we have a single unit which links all individually compiled code files.

Below is 10 minutes youtube video which demonstrates how these both folders look like and how incremental compilation happens.