tag:blogger.com,1999:blog-10106032.post114890250405656580..comments2024-03-14T09:24:53.310+00:00Comments on Entropy Overload: Call vs CallVirt for C# non-virtual instance methodsBarry Kellyhttp://www.blogger.com/profile/10559947643606684495noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-10106032.post-1151252675046682882006-06-25T17:24:00.000+01:002006-06-25T17:24:00.000+01:00I came across the very same thing in the Chrome la...I came across the very same thing in the Chrome language.<BR/>Non-virtual or final virtual methods won't let the compiler emit callvirt, as long as he knows, that the method really is statically linkable.<BR/>Interesting, indeed. :-) Always thought you simply can't call a method on a null reference and nether really tried the nasty. *g*<BR/>But it does in fact work. :-)<BR/>Sample code:<BR/><BR/> Class1 = public class<BR/> public<BR/> method ToString : String; override; final;<BR/> property SomeProperty : String;<BR/> end;<BR/> <BR/>implementation<BR/><BR/>method Class1.ToString : String; <BR/>begin<BR/> if assigned(Self) then <BR/> result := SomeProperty<BR/> else <BR/> result := '(nüschts)';<BR/>end;<BR/><BR/>When you run this code:<BR/><BR/>var noInstance : Class1;<BR/>var isInstance := new Class1(SomeProperty := 'blabla');<BR/><BR/>Console.WriteLine(noInstance.ToString());<BR/>Console.WriteLine(isInstance.ToString());<BR/><BR/>it will print:<BR/>(nüschts)<BR/>blablaAnonymousnoreply@blogger.com