Interface in DotNet

An interface is a reference type object with no implementation. To make our code more reusable, we can provide interfaces to our implementation. Here I have written a sample code for Interface in DotNet using C#.


using System;
using System.Collections.Generic;
using System.Text;

namespace testWinApp
{

interface ItestInterface1
{
void MyFunctionI1();
}

interface ItestInterface2
{
void MyFunctionI2();
}

public class InterfaceTest : ItestInterface1, ItestInterface2
{

public void MyFunctionI1()
{
Console.Write("Test ItestInterface1");
}

public void MyFunctionI2()
{
Console.Write("Test ItestInterface2");
}
}
}


then in button click event,


private void button1_Click(object sender, EventArgs e)
{
InterfaceTest t = new InterfaceTest();
ItestInterface1 i1 = (ItestInterface1)t;
i1.MyFunctionI1();
ItestInterface2 i2 = (ItestInterface2)t;
i2.MyFunctionI2();
}

No comments: