TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Interface Implementation in C#
Ashwani Chaudhary
Jul 22
2015
Code
1.3
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
System;
namespace
ConsoleApplication16
{
public
interface
A
{
void
Hello();
}
public
interface
B
{
void
Hellonew();
}
// Interface C inherite interface A and B
// C is a interface so can not define the function of interface A and B function inherted
// Program class we do implementation
public
interface
C : A , B
{
void
HelloOld();
}
public
class
Program : C
{
void
A.Hello()
// this is the way to implement a function to reduce ambiguity bacuse may be other interface may have same function name
{
Console.WriteLine(
"Interface A is Called"
);
}
void
B.Hellonew()
// same as Interface A
{
Console.WriteLine(
"Interface B is Called"
);
}
void
C.HelloOld()
// same as Interface A
{
Console.WriteLine(
"Interface C is Called"
);
}
}
class
derived : Program
{
static
void
Main(
string
[] args)
{
derived obj =
new
derived();
// this is the way to call interface function by derived class from Program class
((C)obj).Hello();
((C)obj).Hellonew();
((C)obj).HelloOld();
C objnew =
new
Program();
// this is the way to call interface function by Program class
objnew.Hello();
objnew.Hellonew() ;
objnew.HelloOld();
Console.ReadKey();
}
}
}
Output would be same:
// Interface A is Called
// Interface B is Called
// Interface C is Called
// Interface A is Called
// Interface B is Called
// Interface C is Called
C#
Interface