What is the Use Of Interfaces?
For example I have a interface as shown below?

Interface IMyInterface
{
public void MyMethod();
}

class MyClass : IMyInterface
{
public void Mymethod()
{
Some Code
}
}

class Program
{
static void Main(string[] args)
{
MyClass obj = new MyClass();
obj.MyMethod();
}
}

Here What is My Question is?

If i remove Interface and run this code, it will executed then what is the Use
of the interface? Can any one give me the solution for this Problem?

Thanks in Advance!

Answer Posted / sheetal

Offcourse it works if you remove the interface. What is the
purpose of interface is : if you implementing any interface
you need to define all the methods declare in interface in
your class. In this code snippet implement the interface
and do define method in class and compile the program. It
will give you compile time error.

Interface IMyInterface
{
public void MyMethod();
}

class MyClass : IMyInterface
{
//DO NOT DEFINE MyMethod()
}

Is This Answer Correct ?    6 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the types of Polymorphism.

581


What is a static class in c#?

504


What is a race condition?

488


What is the use of 0 in c#?

496


Explain how do I convert a string to an int in c#?

510






What is yield c#?

495


Is static thread safe?

485


What is the use of thread join in c#?

495


What is event delegate in c#?

578


What is value c#?

458


What is the difference between ienumerable and icollection?

468


Why hashtable is thread safe in c#?

491


What is generic in c#?

454


What is private static in c#?

474


What are the types in c#?

499