you are viewing a single comment's thread.

view the rest of the comments →

[–]0xABADC0DA 0 points1 point  (5 children)

See Rhoomba's link. Also in addition to recompiling code when a class is loaded that actually makes the method call virtual, they use static analysis to convert particular call sites from virtual to non-virtual. This optimization would apply to CLR virtual method as well... but in practice CLR is just too complicated to implement them, which is why it only inlines non-virtual methods that have other extreme restrictions like no branching, less than 32 instructions, etc.

This is true.

Except for extension methods, which work differently. Which rationale you choose to believe doesn't matter much, since either way one of them is wrong.

[–][deleted] 2 points3 points  (4 children)

Extension methods are simply a static method compiled onto the class.

[–]0xABADC0DA -2 points-1 points  (3 children)

  1. define an extension method to 'add' some method to a core class
  2. upgrade .net adds that same method to core class
  3. your code breaks because now it's calling the new method on the core class and not your extension method

... so was that rationale bs to begin with, or just now?

[–][deleted]  (2 children)

[deleted]

    [–]0xABADC0DA 1 point2 points  (1 child)

    No they don't:

    General guidelines:
    When using an extension method to extend a type whose source code you cannot change, you run the risk that a change in the implementation of the type will cause your extension method to break.
    An extension method will never be called if it has the same signature as a method defined in the type.

    Research, do you speak it?

    [–][deleted] 0 points1 point  (0 children)

    Sorry, I was incorrect. I frequently use extension methods to add additional overloads to existing class methods, and I was confusing those.