M
Mark Doliner
I'm seeing a weird problem with inheritance/overriding a
method/implicit casting, and someone suggested that I ask about it
here.
I have a class that is inheriting from another class. The base class
has a Write(byte x) method and a Write(ushort x) method. The child
class only overrides Write(ushort x). If I call
ChildClass.Write(byte), the child's ushort method gets called instead
of the base class's Write(byte x) method. Is that normal?
I'm using mono/mcs, but someone tested it in Windows for me and got
the same results. Here's a sample program...
using System;
public class Parent {
public static void DoStuff(byte x) {
Console.WriteLine("In parent method: DoStuff(byte x)");
}
public static void DoStuff(ushort x) {
Console.WriteLine("In parent method: DoStuff(ushort x)");
}
}
public class Childarent {
publicnew static void DoStuff(ushort x) {
Console.WriteLine("In child method: DoStuff(ushort x)");
}
}
public class TestProg {
public static void Main() {
Console.WriteLine("In Main()");
byte val = 4;
Child.DoStuff(val);
}
}
My output from that is:
In Main()
In child method: DoStuff(ushort x)
I would have expected it to be:
In Main()
In parent method: DoStuff(byte x)
Anyone know why that happens?
method/implicit casting, and someone suggested that I ask about it
here.
I have a class that is inheriting from another class. The base class
has a Write(byte x) method and a Write(ushort x) method. The child
class only overrides Write(ushort x). If I call
ChildClass.Write(byte), the child's ushort method gets called instead
of the base class's Write(byte x) method. Is that normal?
I'm using mono/mcs, but someone tested it in Windows for me and got
the same results. Here's a sample program...
using System;
public class Parent {
public static void DoStuff(byte x) {
Console.WriteLine("In parent method: DoStuff(byte x)");
}
public static void DoStuff(ushort x) {
Console.WriteLine("In parent method: DoStuff(ushort x)");
}
}
public class Childarent {
publicnew static void DoStuff(ushort x) {
Console.WriteLine("In child method: DoStuff(ushort x)");
}
}
public class TestProg {
public static void Main() {
Console.WriteLine("In Main()");
byte val = 4;
Child.DoStuff(val);
}
}
My output from that is:
In Main()
In child method: DoStuff(ushort x)
I would have expected it to be:
In Main()
In parent method: DoStuff(byte x)
Anyone know why that happens?