Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is unused code included in a release assembly?

Tags:

c#

With C# in VS2017, if I don't want to comment out a method (or class) which is not used, will it be optimized out and not included in the assembly? Or will it stay in the assembly, taking up space?

like image 463
arteny Avatar asked Nov 28 '18 00:11

arteny


Video Answer


1 Answers

It is in the build because the compiler doesn’t know if it is used or not at runtime; if the assembly is a class library, it can be referenced by other assemblies, and the code can be used (if the class and method are public), so compiler will not optimize the unused code out.

You can use “#if DEBUG” and “#endif” to wrap the unused code, then it will not be compiled into the release build. DEBUG symbol is not defined in release configuration, so the compiler knows you don’t want to include the code in release build.

like image 74
kennyzx Avatar answered Nov 13 '22 16:11

kennyzx