Archive for tag: MyInterface

C# interface instance-like

to work with a library I just found I need to implement a few interfaces first. But some methods seem to be asking for objects that have the type of some interfaces... And if I have an interface called MyInterface I can write things like :

    MyInterface shoe;

It does not really make sense to me. Can somebody teach me this concept ?

Then i have found out very nice solution about C# Inerface, Interfaces establish a contract between a class and the code that calls it. They also allow you to have similar classes that implement the same interface but do different actions or events and not have to know which you are actually working with. This might make more sense as an example so let me use same example as per your link with bit of modification:

using System; 
 
interface IPerl 
{ 
    void Read(); 
} 
 
class Test : IPerl 
{ 
    public void Read() 
    { 
    Console.WriteLine("Read Test"); 
    } 
} 
 
class Test1 : IPerl 
{ 
    public void Read() 
    { 
    Console.WriteLine("Read Test1"); 
    } 
} 
 
class Program 
{ 
    static void Main() 
    { 
    IPerl perl = new Test(); // Create instance of Test. 
    perl.Read(); // Call method on interface output will be different then Test1. 
 
        perl = new Test1(); // Create instance of Test1. 
    perl.Read(); // Call method on interface output will be different then Test. 
 
    } 
} 

Output:

  1. "Read Test"
  2. "Read Test1"

I hope this would help.