namespace InterFaceDemo
{
//defining First interface
interface ISum
{
void sum(int a, int b);
}
//defining second interface which inherits first interface
interface ISubtract:ISum
{
void sub(int a, int b);
}
//defining class which inherits second interface
class NewClass : ISubtract
{
//implementing interface defined methods
public void sum(int a, int b)
{
Console.WriteLine((a+b).ToString());
}
public void sub(int a, int b)
{
Console.WriteLine((a-b).ToString());
}
}
class Program
{
static void Main(string[] args)
{
//creating instance of class
NewClass newclass = new NewClass();
//calling sum() of ISum interface
newclass.sum(4,5);
//calling sub() of ISubtract interface
newclass.sub(5,8);
}
}
}
here i want to know that we have use interface .But we can call add and subtract method with out use interface.then what is need of interface .
Loading

Manoj BhoirPosted May 6, 2015, 2:36 AM
-The only thing it contains are declarations of events, indexers, methods and/or properties.
-The reason interfaces only provide declarations is because they are inherited by classes and structs, which must provide an implementation for each interface member declared.
Interfaces in C# are provided as a replacement of multiple inheritance.
-Because C# does not support multiple inheritance, it was necessary to incorporate some other method so that the class can inherit the behavior of more than one class, avoiding the problem of name ambiguity that is found in C++.
-With name ambiguity, the object of a class does not know which method to call if the two base classes of that class object contain the same named method.
Purposes of Interfaces
-create loosely coupled software
-support design by contract (an implementor must provide the entire interface)
-allow for pluggable software
-allow different objects to interact easily
-hide implementation details of classes from each other
-facilitate reuse of software
When to Use Interfaces
https://msdn.microsoft.com/en-us/library/3b5b8ezk%28v=vs.90%29.aspx
Please refer some links for real time examples:
Real Time example for Interface[^]
Real world examples of abstract classes and interfaces?[^]
Real World Interfaces in C#[^]
Reference : http://www.codeproject.com/Questions/392978/what-is-Interface-in-Csharp-and-why-we-are-using-i