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

What is ispostback c#?

668


What is difference between === and ==?

655


How do you escape a character?

698


What are handlers in c#?

717


What is anonymous method in c#?

651


In languages without exception-handling facilities, we could send an error-handling procedure as a parameter to each procedure that can detect errors that must be handled. What disadvantages are there to this method?

825


What does ienumerable mean?

682


Do events have return type c#?

710


What is string in c# net?

678


What is tuple in c#?

709


Constructor to an arbitrary base constructor?

750


What is difference between assembly and namespace?

658


What is difference between private and static constructor?

677


Give some examples for built in datatypes in c#?

740


Explain the process of Serialization?

752