Can somebody explain how same function on same class behaved different?
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
MyUpperScript mAsdfScript = new MyUpperScript();
IInterface mAsdfInterface = mAsdfScript;
mAsdfScript.Foo();
mAsdfInterface.Foo();
}
}
public class MyUpperScript : MyScript
{
public void Foo()
{
Console.WriteLine("Upper Script");
}
}
public class MyScript : IInterface
{
}
public interface IInterface
{
public void Foo()
{
Console.WriteLine("Interface");
}
}
I was expecting it to write "Upper Script" on both Foo() call
Note that if I derive the second class from the interface too it works the way I expect - both print "Upper Script":
// now deriving for the interface too:
public class MyUpperScript : MyScript, IInterface
{
public void Foo()
{
Console.WriteLine("Upper Script");
}
}
Foo()
fromMyUpperScript
, is there any other way than defining it inMyScript
and overridingMyUpperScript
? Like writingpublic void IInterface.Foo()
at MyUpperScript? – Ziegfeld