I apologise if this has been asked before or if it’s now answerable.
I’m working with a big code base (most of it was written by another Software Engineer before I started work on it, standard affair). I’ve added quite a lot of code to this project, most of it has been wrapping existing methods into objects/libraries for ease of reuse. By wrapped, I mean that I’ve copied them into a new object/library, edited them to make them work as standalone objects or libraries (where applicable) and called/instantiated them where appropriate in the original code base.
For instance, instead of a massive method (foo) that provides some one time function being included in one of the .cs files for the main exe, I’ve wrapped it up in it’s own class and separated it, so that it can (theoretically) be used in other projects or different parts of the solution.
IE going from this:
/* somewhere in Main */
void Main ()
{
...
foo (arg1, arg2, arg3)
...
}
foo (arg1, arg2, arg3)
{
//some massive functionality
}
to this:
/* somewhere in Main or any other part of the code base */
Foo myFoo = new Foo (arg1, arg2, arg3);
myFoo.DoSomething();
today, I’ve started looking through the code base and commenting out all of the old methods (the one’s that I’ve wrapped into objects/libraries) in an effort figure out which of them I can remove from the original code base. I’m using version control software, and I’m doing them a few at a time, then re-compiling and performing some testing to check that I’ve not broken anything.
My question is this:
When I compile, is the C# compiler (using version 3.5) smart enough that it wont include methods/functions/objects/libraries that never get called, instantiated or used?
I’m still going to tiding up/clean out the now deprecated methods/functions, but I’m just wondering whether the compiler can produce outputs that don’t contain these methods/functions.
TL;DR: if I have the following Main.cs
/*using statements here*/
public void Main ()
{
foo();
}
foo ()
{
/*Some commands*/
}
bar()
{
/*Never called*/
}
Does my compiled output contain any references to bar?
The C# compiler will include all methods, even those that are never called, and even if they are private.
So yes, the compiled output defines the method
bar, and its body is compiled and present in your binary.This means that you can break code by just removing a method, even if the result compiles: that method could be invoked by reflection only. So you’re right about also doing the testing afterwards.