Possible Duplicate:
Why C# implements techniques as non-virtual automatically?

It might be a lot more less try to define which techniques aren't overideable rather than which are overideable because (a minimum of for me personally), when you are creating a category, you do not care if it is beneficiaries will override your techniques or otherwise...

So, why techniques in C# aren't instantly virtual? What's the good sense within this?

Anders Hejlsberg clarified that question in this interview and that i quote:

You will find several reasons. The first is performance. We are able to realize that as people write code in Java, they forget to mark their techniques final. Therefore, individuals techniques are virtual. Because they are virtual, they do not be as good. There's just performance overhead connected with as being a virtual method. That's one problem.

A far more important problem is versioning. You will find two ways of thinking about virtual techniques. The academic school of thought states, "Everything ought to be virtual, because I may want to override it at some point." The practical way of thinking, which will come from building real programs running in the real life, states, "We have reached be real careful by what we make virtual."

Whenever we make something virtual inside a platform, we are making a great deal of promises about how exactly it evolves within the future. For any non-virtual method, we promise that after you refer to this as method, x and y may happen. Whenever we create a virtual method within an API, we not just promise that after you call this process, x and y may happen. We also promise that after you override this process, we'll refer to it as within this particular sequence regarding other ones and also the condition is going to be in every part invariant.

Each time you say virtual within an API, you're developing a call back hook. As an OS or API framework designer, you need to be genuine careful about that. You wouldn't want customers overriding and connecting at any arbitrary reason for an API, since you cannot always make individuals promises. The ones may not completely understand the promises they are earning once they make something virtual.

You need to care which people could be overridden in derived classes.

Determining which techniques to create virtual ought to be a deliberate, well-thought-out decision - not something which happens instantly - just like every other choices concerning the public the surface of your API.

Past the design and clearness reasons a non-virtual technique is also technically better for a few reasons:

  • Virtual techniques take more time to call (since the runtime must travel through the virtual research table to obtain the actual approach to call)
  • Virtual techniques can not be inlined (since the compiler does not know at compile time which method will ultimately be known as)

Therefore unless of course you've specific intentions to override the technique it is best for this to become non-virtual.

Convention? Anything, I'd think. I understand Java instantly makes techniques virtual, while C# doesn't, so there's clearly some disagreement at some level regarding what's better. Personally, I favor the C# default - take into account that overriding techniques is less frequent than not overriding them, therefore it would appear more concise to define virtual techniques clearly.

Therefore it is obvious whether you are permitting overriding or perhaps a method or forcing hiding of the method (through the new keyword).

Forcing you to definitely add the keyword removes any ambiguity that could be there.

See also the solution of Anders Hejlsberg (the inventor of C#) at A Conversation with Anders Hejlsberg, Part IV.

In my opinion there's an efficiency problem along with the reasons others have published. There's you don't need to spend the cpu cycles to search for an override when the technique is not virtual.