2

Does the structure of a C# file affect what is compiled?

For example, would the order of members (in terms of where in the file it exists) affect the compiled class?

StuperUser
  • 10,555
  • 13
  • 78
  • 137

3 Answers3

5

According to a blog entry from Eric Lippert I read the other day, the compiler does not guarantee two compilations of the same source to end up the same IL. This wasn't about method ordering, though, but I think that applies too - so, I don't think it is safe to assume any method ordering in the compiled class. I also wouldn't expect any connection between source file method ordering and IL method ordering.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • 2
    http://blogs.msdn.com/b/ericlippert/archive/2012/05/31/past-performance-is-no-guarantee-of-future-results.aspx is a link to the blog. – RB. Jun 22 '12 at 09:24
2

While in most cases the order is unimportant, this is not the case with anonymous types:

new{a=1,b=2}

creates a different, incompatible type to

new{b=2,a=1}
spender
  • 117,338
  • 33
  • 229
  • 351
  • Do you know of any cases other than serialisation (http://stackoverflow.com/questions/3282991/serialize-deserialize-objects-order-of-fields-matters), where the order is important? – StuperUser Jun 22 '12 at 09:45
0

there is no guarantee for the order of the members.

Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154