interface.htm
  • Interfaces is place or signature holder  a group of related behaviors that can belong to any class or struct. It is an effort to keep similar objects together.
    • Example
       interface team1
      {
      void MyFunction(string str);//this is a method
      }
  • Interfaces may contain methods, properties, events, indexers, or any combination of those four member types.
  •  An interface can not contain fields.
  • The members of the Interfaces are  public in default.
  • Classes and structs can inherit from interfaces in a manner similar to how classes can inherit a base class or struct, with two exceptions:
    • A class or struct can inherit more than one interface.
    • When a class or struct inherits an interface, it inherits only the method names and signatures, because the interface itself contains no implementations. For example:

      public
      class Minivan : Car, IComparable
      {
          public int CompareTo(object obj)
          {
              //implementation of CompareTo
              return 0;  //if the Minivans are equal
          }
      }
       
To implement an interface member,
  • all the corresponding member of the class must be public, non-static, and should have the same name and signature of the interface member which a class inherits .
  •  Properties and Indexers on a class are allowed to define extra accessors for a property or indexer defining an interface.
    • For example, an interface may declare a property with a get accessor, but the class implementing the interface can declare the same property with both a get and set accessor.
      • Interface can only have get
      • class and other members can have both get & set
    •  However, if the property or indexer uses explicit implementation, the accessors must match.
      • Interfaces and interface members are abstract; interfaces do not provide a default implementation. For more information, see Abstract and Sealed Classes and Class Members.
      • The IComparable interface announces to the user of the object that the object can compare itself to other objects of the same type, and the user of the interface does not need to know how this is implemented.

    Interfaces can inherit other interfaces.

    • It is possible for a class to inherit an interface multiple times, through base classes or interfaces it inherits. In this case, the class can only implement the interface once, if it is declared as part of the new class. If the inherited interface is not declared as part of the new class, its implementation is provided by the base class that declared it. It is possible for a base class to implement interface members using virtual members; in that case, the class inheriting the interface can change the interface behavior by overriding the virtual members. For more information on virtual members, see Polymorphism.
    //csc interface_1.cs
using System;

namespace soccer
{
	interface team1
	{
		void MyFunction(string str);
	}
	interface team2
	{
		void MyFunction(string str);
	}
	class Test : team1,team2
	{
	  public void MyFunction(string str)
	 {
	  Console.WriteLine("Hmm, I am not sure where to go : " + str);
	  }
	}
	class AppClass
	{
		public static void Main(string[] args)
		{
			string str = "player 1";
			Test t=new Test();
			team1 t1=(team1)t;
			t1.MyFunction(str);
			team2 t2=(team2)t;
			t2.MyFunction(str);
		}
	}
}
In the above scenario, we can use dot operator to point a method having similar name.
//csc interface_2.cs
using System;

namespace soccer
{
	interface team1
	{
		void MyFunction(string str);
	}
	interface team2
	{
		void MyFunction(string str);
	}
	class Test : team1,team2
	{
		//can't use public void team1.MyFunction()
		void team1.MyFunction(string str)
		{
			Console.WriteLine("Team 1 invites " + str);
		}
		void team2.MyFunction(string str)
				{
			Console.WriteLine("Team 2 invites " + str);
		}
	}
	class AppClass
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Player for team 1 : ");
			string s1 = Console.ReadLine();
			Test t=new Test();
			team1 t1=(team1)t;
			Console.WriteLine("Player for team 2 : ");
			string s2 = Console.ReadLine();
			t1.MyFunction(s1);
			team2 t2=(team2)t;
			t2.MyFunction(s2);
		}
	}
}