What exactly is the purpose of the 'obj' directory in .NET?
"obj" folder is used to store temporary object files and other files used to create the final binary.
The bin and obj folders are usually safe to delete since they are automatically generated when the solution/project is being build by Visual Studio/MSBuild.
Object files contain relocatable machine code that is generated from your source code. Visual Studio creates one . obj file per each . cpp file. Then depending on the project (executable, static/dynamic library) you can get relocatable or non-relocatable code.
The obj folders are really intended to hold intermediate things. But in C# projects, there will be no *.obj files that serve as intermediate compilation result of source code files like C/C++. Why is there a copy of the DLL or EXE file in the obj folder, as well as the bin folder?
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.
The default output path for any project is Visual studio is bin/Debug, but I have noticed that obj folder is also generated which again contains dll and pdb files. Can someone tell me why is this folder generated?
Now, as the files are intermediate, you don't need to look into this directory. Well, almost. Sometimes, this is useful to learn how some things work. One example is this: WPF project generates some common code and compiles with that generated code, so it is placed in the "obj" sub-directory. That includes the Main method and some other stuff.
The "obj" folder is used to store temporary object files and other files used in order to create the final binary during the compilation process.
The "bin" folder is the output folder for complete binaries (assemblies).
In addition to splattne's answer, I believe the reason for having it (and not cleaning it up after the build) is to support incremental compilation. If you've already compiled 100 classes and change one of them, it's much more efficient to just recompile the code for the one changed class and reassemble the exe/dll from a mixture of the new and old code.
Of course, incremental compilation is a lot more complex than just that - it has to keep track of everything so it can detect when it needs to recompile a class even if that class itself hasn't changed. (e.g. if a new overload becomes available in a class - some callers may need to be recompiled.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With