Implementing an interface

suggest change

An interface is used to enforce the presence of a method in any class that ‘implements’ it. The interface is defined with the keyword interface and a class can ‘implement’ it by adding : InterfaceName after the class name. A class can implement multiple interfaces by separating each interface with a comma.

: InterfaceName, ISecondInterface

public interface INoiseMaker
{
    string MakeNoise();
}

public class Cat : INoiseMaker
{
    public string MakeNoise()
    {
        return "Nyan";
    }
}

public class Dog : INoiseMaker
{
    public string MakeNoise()
    {
        return "Woof";
    }
}

Because they implement INoiseMaker, both cat and dog are required to include the string MakeNoise() method and will fail to compile without it.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Interfaces:
* Implementing an interface

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
40 Interfaces
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script