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

How do we achieve encapsulation in c#?

475


What is the use of console?

486


What’s thread.sleep() in threading ?

505


What is the difference between ienumerable and enumerator?

446


What is xml c#?

481






What is regex c#?

482


If the original method is not static you can declare an override method to be static or not?

497


What does void mean in c#?

507


What is difference between override and new in c#?

505


Illustrate race condition?

474


What is the difference between abstract and abstraction?

435


What is multithreading? What are the problems that comes with multithreading and how to manage them?

547


Is type nullable c#?

517


How is lazy loading achieved?

491


Explain streamreader/streamwriter class?

536