Logo 
Search:

C# Forum

Ask Question   UnAnswered
Home » Forum » C#       RSS Feeds

Example for Interface in C#

  Asked By: Sam    Date: May 07    Category: C#    Views: 6455
  

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

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Dep Tran     Answered On: May 07

Please let me know how much it was useful to you?

 
Didn't find what you were looking for? Find more on Example for Interface in C# Or get search suggestion and latest updates.




Tagged: