Interfaces
Defines what interfaces are and outlines its properties
An interface looks similar to a class. It contains methods and properties but has no implementation. We do not specify what the methods and properties will actually do. The reason is that classes and structures inherit them and they provide the actual implementation for each interface member defined.
The syntax for defining interface is as follows:
interface interface-name
{
//members of the interface
}
Commonly, the name of the interface starts with the prefix `I' (though it is not a strict rule). The following example illustrates how to define an interface:
interface IMyInterface { void SomeMethod(); }
Notice that the SomeMethod() method does not have any implementation. This is because we do not use an interface directly in our program. We need a class or structure that will implement the interface:
class MyClass : IMyInterface { public void SomeMethod() { Console.WriteLine("This is a method"); } }
Here, the class MyClass implements the SomeMethod() method of the interface. An interface is a type of contract that specifies that the class or structure inheriting the interface must implement its members.
From the previous chapters, we have so far covered the C# language fundamentals required for a beginner. Remember that there is a lot more to the language. However, the topics covered so far us will prepare us for the next stage of C# programming - windows programming.