I have collected sample examples for Interface in C#.
Check different sample cases for interface - their outputs and errors.
Case1.
class Program
{
interface intfDemo
{
void Demo();
}
private class clsDemo:intfDemo
{
public void Demo()
{
Console.Write("HelloWorld");
}
}
static void Main(string[] args)
{
clsDemo cls = new clsDemo();
cls.Demo();
}
}
Output:
HelloWorld
Case2.
class Program
{
interface intfDemo
{
void Demo();
string strDemo();
}
private class clsDemo:intfDemo
{
public void Demo()
{
Console.Write("HelloWorld");
}
}
static void Main(string[] args)
{
clsDemo cls = new clsDemo();
cls.Demo();
}
}
Output:
Error:'ConsoleApp.Program.clsDemo' does not implement interface member 'ConsoleApp.Program.intfDemo.strDemo()'
Case3:
class Program
{
interface intfDemo
{
void Demo();
}
interface intfDemo2
{
void Demo(string s);
}
interface intfCombine : intfDemo, intfDemo2
{
}
class clsDemo : intfCombine
{
public void Demo()
{
Console.Write("HelloWorld");
Console.Read();
}
public void Demo(string str)
{
Console.Write("HelloWorld");
Console.Read();
}
}
static void Main(string[] args)
{
clsDemo cls = new clsDemo();
cls.Demo();
}
}
Output:
HelloWorld