Does compiler omit unused methods?

  • Thread starter Thread starter Michael A. Covington
  • Start date Start date
M

Michael A. Covington

Does the C# compiler omit methods that are never called?

I'm constructing a class with many methods, one of which is probably
incompatible with Mono. I'm wondering it it will be omitted from the .exe
in programs that never refer to it. Or is that wishful thinking?
 
Michael said:
Does the C# compiler omit methods that are never called?

I'm constructing a class with many methods, one of which is probably
incompatible with Mono. I'm wondering it it will be omitted from the .exe
in programs that never refer to it. Or is that wishful thinking?

No. It can not. One reason is that you can reference an exe just
like a dll.

Arne
 
Michael said:
I'm constructing a class with many methods, one of which is probably
incompatible with Mono. I'm wondering it it will be omitted from the .exe
in programs that never refer to it. Or is that wishful thinking?
Yes. For one thing, the compiler is certainly required to *compile* it, even
if it somehow decided not to link it into the assembly.

If it can be compiled regardless of platform but not called at runtime on
every platform, then just don't call it at runtime on those platforms, or
check in the method itself and throw a NotSupportedException. If it cannot
even be compiled on some platforms, then use #if directives to leave the
method out altogether.
 
Does the C# compiler omit methods that are never called?
It could omit private methods that are not called. Other methods
might be called from code not present at compile time and so must be
included.
I'm constructing a class with many methods, one of which is probably
incompatible with Mono. I'm wondering it it will be omitted from the .exe
in programs that never refer to it. Or is that wishful thinking?
Check for Mono and throw an exception, or program round the issue.

rossum
 
rossum said:
It could omit private methods that are not called. Other methods
might be called from code not present at compile time and so must be
included.

Even the private stuff could be accessed via reflection.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top