3

Inspired by this SO question:

Why doesn't C# have a keyword for non-virtual calling convention?

EDIT: I mean "why is there no keyword for non-virtual IL calls, given that the C# compiler always uses IL virtual calls by default"?

Community
  • 1
  • 1
Mau
  • 14,234
  • 2
  • 31
  • 52

2 Answers2

3

Take a look at why c# implements methods as non-virtual by default?

To quote Anders Hejlsberg

There are several reasons. One is performance. We can observe that as people write code in Java, they forget to mark their methods final. Therefore, those methods are virtual. Because they're virtual, they don't perform as well. There's just performance overhead associated with being a virtual method. That's one issue.

A more important issue is versioning. There are two schools of thought about virtual methods. The academic school of thought says, "Everything should be virtual, because I might want to override it someday." The pragmatic school of thought, which comes from building real applications that run in the real world, says, "We've got to be real careful about what we make virtual."

When we make something virtual in a platform, we're making an awful lot of promises about how it evolves in the future. For a non-virtual method, we promise that when you call this method, x and y will happen. When we publish a virtual method in an API, we not only promise that when you call this method, x and y will happen. We also promise that when you override this method, we will call it in this particular sequence with regard to these other ones and the state will be in this and that invariant.

Every time you say virtual in an API, you are creating a call back hook. As an OS or API framework designer, you've got to be real careful about that. You don't want users overriding and hooking at any arbitrary point in an API, because you cannot necessarily make those promises. And people may not fully understand the promises they are making when they make something virtual.

Source: http://www.artima.com/intv/nonvirtual.html

Community
  • 1
  • 1
Zaki
  • 6,997
  • 6
  • 37
  • 53
  • 2
    This is somewhat off-topic. The question is why can't we call a method through a non-virtual IL call. Calls to non-virtual methods in C# still use an IL virtual call instruction, as other have said. I wonder if people just vote the longest post up, or they actually read them. – Mau Jul 31 '10 at 17:38
  • @Mau, I wouldnt call it off-topic. This answers the question "Why doesn’t C# have a non-virtual calling convention?" and thats the title of the question, well atleast before the edit. – Zaki Aug 01 '10 at 05:06
2

This might explain it: Link

Shortly: call instruction can accept null as this pointer (as it is in C++). This is errorsome and C# team decided to use callvirt wherever it is possible so that calls on null pointers throw NullReferenceException

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Andrey
  • 59,039
  • 12
  • 119
  • 163
  • 1
    I think this addresses another issue than the one the OP asks about. – Dirk Vollmar Jul 31 '10 at 14:58
  • @0xA3 @Hans Passant i had a feeling that question was stated incorrectly by author. look at edit, i guessed right – Andrey Jul 31 '10 at 19:08
  • Too bad, would have been a great question. – Hans Passant Aug 02 '10 at 09:52
  • The authors of C# might not have liked the idea of being able to pass a null "this" pointer, but there are some cases where it could have improved legibility [e.g. being able to say SomeString.IsNullOrEmpty instead of String.IsNullOrEmpty(SomeString)]. – supercat Oct 27 '11 at 01:12
  • @supercat it would improve few cases and mess up a lot of other. – Andrey Oct 27 '11 at 15:12
  • @Andrey: My preference would be to allow a means by which extension methods that operated on objects of a particular class could be declared within that class and obey the scoping rules of that class. Incidentally, if I had my druthers, struct members other than constructors would be forbidden from mutating "this", but structs could declare extension methods which took their "this" parameter by reference. Such a change would break existing .net code, of course, but one could deprecate changes to "this" to allow code to migrate toward the safer syntax. – supercat Oct 27 '11 at 16:06