Information Technology Reference
In-Depth Information
Example with a Simple Interface
The following code declares an interface named IIfc1 , which contains a single method named
PrintOut . Class MyClass implements interface IIfc1 by listing it in its base class list and supply-
ing a method named PrintOut that matches the signature and return type of the interface
member. Main creates an object of the class and calls the method from the object.
interface IIfc1 Semicolon in place of body // Declare interface
{
void PrintOut(string s);
}
Implement interface
class MyClass : IIfc1 // Declare class
{
public void PrintOut(string s) // Implementation
{
Console.WriteLine("Calling through: {0}", s);
}
}
class Program
{
static void Main()
{
MyClass mc = new MyClass(); // Create instance
mc.PrintOut("object."); // Call method
}
}
This code produces the following output:
Calling through: object.
Search WWH ::




Custom Search