To start with maybe you would like to run the following examples which
highlights some basic differences.
It might look a bit odd if you are not used to these tricks. But it's simple
anyway
-----------START OF EXAMPLE ------------------
using System;
namespace InVirtual
{
class Program
{
static void Main(string[] args){
var x = new MyClass();
var y = new MyDerivedClass();
var z = new MyOtherDerivedClass();
x.InterafaceMethod();
y.InterafaceMethod();
z.InterafaceMethod();
Console.WriteLine("-- classes array --");
var array = new MyClass[] { x, y,z };
foreach (var a in array) {
a.InterafaceMethod();
}
Console.WriteLine("-- interfaces array --");
var interfaces = new ISomething[] { x, y ,z};
foreach (var i in interfaces){
i.InterafaceMethod();
}
}
}
public interface ISomething{
void InterafaceMethod();
}
public class MyClass : ISomething{
public virtual void VirtualMethod(){
Console.WriteLine("MyClass.VirtualMethod");
}
#region ISomething Members
public void InterafaceMethod(){
Console.WriteLine("MyClass: ISomething");
}
#endregion
}
public class MyDerivedClass : MyClass{
public void InterafaceMethod(){
Console.WriteLine("MyDerivedClass: ISomething");
}
}
public class MyOtherDerivedClass : MyClass, ISomething{
public void InterafaceMethod(){
Console.WriteLine("MyOtherDerivedClass: ISomething");
}
}
}
-----------END OF EXAMPLE ------------------
On Mon, Jul 13, 2009 at 2:25 PM, Knight Commander
<[email protected]>wrote:
> Hi Cecilers :)
> Is there any way to differ between a C# virtual method (not IL virtual) and
> interface implementation method?
> To clarify what I mean, in the example below, what is the difference
> between "InterfaceMethod" and "VirtualMethod" in "MyClass" class?
>
> Here is the example:
>
> -----------START OF EXAMPLE ------------------
>
> public interface ISomething{
> void InterafaceMethod();
> }
>
> public class MyClass:ISomething{
>
> public void InterfaceMethod()
> {
> }
>
> public virtual void VirtualMethod()
> {
> }
> }
>
> -----------END OF EXAMPLE ------------------
>
> thank you all
>
>
> CECIL is GREAT!
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---