Click here to Skip to main content
15,041,266 members
Articles / Programming Languages / C#
Article
Posted 11 Jun 2018

Tagged as

Stats

30.6K views
23 bookmarked

C# bin vs obj Folder

Rate me:
Please Sign up or sign in to vote.
4.62/5 (36 votes)
21 Jun 2021CPOL2 min read
bin vs obj folder in C#

When you 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 is why not just one folder and why two folders?

Image 1

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 into 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 into a single unit assembly which can be a DLL or EXE.
Image 2

If you compare both the folders (see the 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 separate compiled code files for each source code file.

Image 3

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

When we work with big projects, we will have a 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.

Image 4

In summary, in “obj” folder, we 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 a 10 minute YouTube video which demonstrates how both these folders look like and how incremental compilation happens.

Image 5

For Further reading do watch  the below interview preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
GeneralWhy this article get so many upvotes? Pin
wmjordan27-Jun-21 21:04
professionalwmjordan27-Jun-21 21:04 
The statements about the stuff in the obj folder are so wrong that I have not voted it at all.

1:
There is no such thing that "We have two folders because the compilation process goes through two steps, compiling and linking.", or that "In the “obj” folder we have entry of each code file compilation."
In C/C++ projects, they are true. In C# projects. NO such entries for each code files! This article is talking about C# projects. So the above statement is wrong.

2:
The obj folder contains compiled assembly files. But they are not usually used to "link files together". Althought we can't work without a linker in C/C++ projects. In C# projects, it is no such type of story. We call the cs.exe file (C# compiler), afterwards the assembly is ready if the compilation is successful. No linker is required.

I wonder whether the author has event attempted to examine those "*.AssemblyInfoInputs.cache", "*.CoreCompileInputs.cache", "*.FileListAbsolute.txt" files before jumping to the conclusion that they are linked into the final assembly files in the Bin folder.

3:
The article has also missed a very important folder in recent VS solutions.

There's a .vs folder in each solution folder, which is by default hidden unless you check the show hidden files in the Windows Explorer options. The folder has a slnx.sqlite file, which is an SQLite database. Open it with an SQLite database browser, you can see many things.

For instance, the FileScannerCache and FileSystemEntity table contain timestamps for code files and folders. "So we can know from the same which files exactly have changed, thus making compiling fast."--the magic is not in the obj folder, it is here. And those entries are not used for making compiling fast, but to determine whether files in a project is modified and consequently a compilation is required after a successful compilation is already there.

4:
Finally, this article is of wrong type. The quality of the article and information at most fits being a tip--if it is correct.
GeneralRe: Why this article get so many upvotes? Pin
Shivprasad koirala27-Jun-21 21:26
MemberShivprasad koirala27-Jun-21 21:26 
GeneralRe: Why this article get so many upvotes? Pin
wmjordan28-Jun-21 21:49
professionalwmjordan28-Jun-21 21:49 
QuestionDuplicate DLL or EXE files Pin
JNygren23-Jun-21 17:05
MemberJNygren23-Jun-21 17:05 
AnswerRe: Duplicate DLL or EXE files Pin
wmjordan27-Jun-21 21:08
professionalwmjordan27-Jun-21 21:08 
Questionneed to remove all obj folders Pin
martinrj3022-Jun-21 22:39
Membermartinrj3022-Jun-21 22:39 
GeneralRe: need to remove all obj folders Pin
Kent K23-Jun-21 9:46
professionalKent K23-Jun-21 9:46 
AnswerRe: need to remove all obj folders Pin
tbayart28-Jun-21 11:50
professionaltbayart28-Jun-21 11:50 
GeneralMy vote of 5 Pin
rich.v222-Jun-21 11:08
Memberrich.v222-Jun-21 11:08 
Generaldoes Visual Studio use NMAKE utility to implement this? Pin
Southmountain16-Jun-18 9:24
MemberSouthmountain16-Jun-18 9:24 
GeneralRe: does Visual Studio use NMAKE utility to implement this? Pin
Richard MacCutchan21-Jun-21 22:40
mveRichard MacCutchan21-Jun-21 22:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.