Click here to Skip to main content
16,019,983 members
Home / Discussions / C#
   

C#

 
GeneralRe: Export Issue Pin
MumbleB2-May-08 5:45
MumbleB2-May-08 5:45 
AnswerRe: Export Issue Pin
Big Daddy Farang2-May-08 5:47
Big Daddy Farang2-May-08 5:47 
GeneralRe: Export Issue Pin
MumbleB2-May-08 5:54
MumbleB2-May-08 5:54 
GeneralRe: Export Issue Pin
Big Daddy Farang2-May-08 6:48
Big Daddy Farang2-May-08 6:48 
GeneralRe: Export Issue Pin
MumbleB2-May-08 10:36
MumbleB2-May-08 10:36 
AnswerRe: Export Issue Pin
MumbleB2-May-08 10:47
MumbleB2-May-08 10:47 
QuestionCall a virtual method non-virtually.. Pin
ptr2void2-May-08 2:19
ptr2void2-May-08 2:19 
AnswerRe: Call a virtual method non-virtually.. Pin
Simon P Stevens2-May-08 5:13
Simon P Stevens2-May-08 5:13 
IL has two ways of calling a method. Call and CallVirt. CallVirt basically does some extra checks and stuff to make sure the object the method is being called on is not null, and it also then works out exactly which method to call by looking at the object hierarchy and calling the appropriate virtual method for the actual object passed.

If you write the IL by hand, there is no reason why you couldn't call virtual methods with the Call command, what would be missed out though is the polymorphic part of the call, so exactly the method you specify on the type you specify will be called, rather than the CLR looking at the object hierarchy and finding the appropriate virtual method.

Lets see. Say you had two Classes.

BaseClass and SuperClass (which inherits from BaseClass)

BaseClass defines a method called "TestMethod", SuperClass overrides that method and provides it's own implementation.

In C#, if you create a superclass object, cast it to a BaseClass and then call TestMethod, because it's called with CallVirt, the CLR will locate and call the TestMethod on the SuperClass, and call that, despite the fact that the objects was cast to a BaseClass. This is because even though it's been cast to a BaseClass, the object is still a SuperClass object.

Now, if you edited the IL and changed the CallVirt to a Call command, (your now calling a virtual method non-virtually) that part of the CLR processing wouldn't happen, and it would call the BaseClass method, because that's the Type that has been provided in the call.

If you only ever write in C#, it's nothing really to worry about. C# compiles all instance method calls to a CallVirt command, even if they are not virtual. (they are therefore non-virtual methods being called virtually, which doesn't really matter at all).

This is only ever worth worrying about if you have code that references your code that is not written in C#, it may call your non virtual methods with Call, which is fine to begin with, but it means if you then change your C# non-virtual method to a virtual one, the calling code will be calling a virtual method with the Call command, which as shown above, gives unexpected results.

(Try defining the classes I described, compile it, and use ILDasm to output the IL. You can manually change the callvirt to call and use ILAsm to reassemble the IL. The behaviour of the program changes as I described)

Simon

GeneralRe: Call a virtual method non-virtually.. Pin
N a v a n e e t h2-May-08 17:01
N a v a n e e t h2-May-08 17:01 
GeneralRe: Call a virtual method non-virtually.. Pin
Simon P Stevens5-May-08 22:04
Simon P Stevens5-May-08 22:04 
GeneralRe: Call a virtual method non-virtually.. Pin
ptr2void3-May-08 2:56
ptr2void3-May-08 2:56 
AnswerRe: Call a virtual method non-virtually.. Pin
S. Senthil Kumar2-May-08 5:24
S. Senthil Kumar2-May-08 5:24 
QuestionVPN Connection through C#.net ???????? Pin
Seema Gosain2-May-08 1:37
Seema Gosain2-May-08 1:37 
AnswerRe: VPN Connection through C#.net ???????? Pin
Vasudevan Deepak Kumar2-May-08 1:45
Vasudevan Deepak Kumar2-May-08 1:45 
QuestionMove to Next Column DataGridView Pin
MrColeyted2-May-08 0:30
MrColeyted2-May-08 0:30 
AnswerRe: Move to Next Column DataGridView Pin
J a a n s2-May-08 1:19
professionalJ a a n s2-May-08 1:19 
AnswerRe: Move to Next Column DataGridView Pin
Gopal.S2-May-08 1:31
Gopal.S2-May-08 1:31 
GeneralRe: Move to Next Column DataGridView Pin
MrColeyted2-May-08 4:55
MrColeyted2-May-08 4:55 
Questionicon menu Pin
Bhim Prakash Singh1-May-08 23:50
Bhim Prakash Singh1-May-08 23:50 
AnswerRe: icon menu Pin
Simon P Stevens1-May-08 23:58
Simon P Stevens1-May-08 23:58 
GeneralRe: icon menu Pin
Bhim Prakash Singh2-May-08 1:48
Bhim Prakash Singh2-May-08 1:48 
GeneralRe: icon menu Pin
Simon P Stevens2-May-08 3:06
Simon P Stevens2-May-08 3:06 
Questionhow to draw a graph using data in the DB in .NET 2.0 Pin
prasadbuddhika1-May-08 22:54
prasadbuddhika1-May-08 22:54 
AnswerRe: how to draw a graph using data in the DB in .NET 2.0 Pin
Simon P Stevens1-May-08 23:56
Simon P Stevens1-May-08 23:56 
AnswerRe: how to draw a graph using data in the DB in .NET 2.0 Pin
parth.p2-May-08 4:36
parth.p2-May-08 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.