Is there an equivalent of MyClass?

Published 12 March 04 02:30 AM

No, C# doesn't have an equivalent of VB.NET's MyClass keyword. If you want to guarantee not to call an overridden version of a method, you need to make it non-virtual in the first place.

[Author: Jon Skeet]

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Adam Weigert said on March 12, 2004 5:14 AM:
Isn't MyClass just something like

public class MyObject {
public virtual void DoSomething() {
Console.WriteLine("I did something!");
}

public void Process() {
((MyObject)this).DoSomething(); // MyClass.DoSomething()
}
}

?
# Jon Skeet said on March 12, 2004 5:22 AM:
No - just casting doesn't remove the fact that the call is made polymorphically. (i.e. if a subclass overrides DoSomething, then calls Process, it's the subclass's version of DoSomething which will be called.)
# Adam Weigert said on March 12, 2004 5:40 AM:
Wow, I did not realize that... Is there any way to actually call this classes method instead of the overriden one?
# Jon Skeet said on March 12, 2004 9:45 AM:
Only by making it non-virtual.
# Adam Weigert said on March 12, 2004 10:03 AM:
Is there a reason this was added to VB.NET and not C#? I looked at the difference between the code, the VB.NET IL does a call MyObject.Process while the C# IL does a callvirt MyObject.Process

Obvious why this happens, but what about adding it to C#, or is a bad feature to have? I have never used it, just wondering why ...
# Kevin Westhead said on March 12, 2004 10:06 AM:
Do you know if MyClass is something that the VB team went out of their way to provide by doing lots of work or is this natively supported in CIL?
# Panos Theofanopoulos said on March 12, 2004 1:34 PM:
Adam :
You can use "base" but you need a new class :)

public class YourObject : MyObject {
public void Process() {
base.DoSomething(); // base and this make a lot of difference here
}
}

public class HisObject : YourObject {
public override void DoSomething() {
Console.WriteLine("Process never calls it!");
}

# Daniel O'Connell said on June 9, 2004 9:31 PM:
it's the subclass's version of DoSomething which will be called

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker