4

While looking through some dll's with ILSpy I came across the following code:

void RenderFiles(List<List<string>> pdfFiles)
{
  int num;
  for (int i = 0; i < pdfFiles.Count; i = num + 1)
  {
    // ....
    num = i;
  }
}

The introduction of the num variable seems strange to me. Why would the compiler introduce an extra local variable?

The original code is just a simple loop, although it uses a count variable and not a foreach enumerator:

void RenderFiles(List<List<string>> pdfFiles)
{
  for (int i = 0; i < pdfFiles.Count; i++)
  {
  }
}
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
  • 4
    why downvote and close vote? this is not an opinion based question... – Serve Laurijssen Jan 04 '16 at 14:11
  • ILSpy is not the best decompiler. I find that Reflector produces cleaner results. Decompilation is not exact. The decompiler guesses what the source was. – usr Jan 04 '16 at 14:16
  • 11
    Did you verify that the extra variable is actually present in the CIL? This could just be a 'feature' of the decompiler. – Galax Jan 04 '16 at 14:18
  • num is not referenced anywhere else – Serve Laurijssen Jan 04 '16 at 14:23
  • 2
    I would recommend Jetbrains [DotPeek](https://www.jetbrains.com/decompiler/). Best free decompiler i can think of. – CSharpie Jan 04 '16 at 14:24
  • 8
    what is the IL? that's the important bit here. The decompiled stuff is usually a red herring – Marc Gravell Jan 04 '16 at 14:24
  • @Csharpie Dotpeek just shows the loop as it originally was. I always thought any decompiler would show the IL as it was. But it was indeed a red herring. – Serve Laurijssen Jan 04 '16 at 14:35
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Jan 04 '16 at 15:52
  • Was this assembly in Debug or Release? BTW, it is not 100% possible to decompile code back to the original source. Some assumptions are made and those assumptions are based on what each decompiler/compiler vendors decides. Here is a really old example of some code that all compiled into the same IL. http://stackoverflow.com/questions/2447559/c-sharp-does-function-get-called-for-each-iteration-of-a-foreach-loop/2447646#2447646 – Matthew Whited Jan 04 '16 at 17:33

0 Answers0