How would you provide this control validation capability? This is where interfaces excel. Here's a simple interface example containing a single method named Validate: -
interface IValidate { bool Validate(); }
Now you can document the fact that if the control implements the IValidate interface, the control can be validated.
Let's examine a couple of aspects of the preceding code snippet. First, you don't need to specify an access modifier such as public on an interface method. In fact, prepending the method declaration with an access modifier results in a compile-time error. This is because all interface methods are public by definition. (C++ developers might also notice that because interfaces, by definition, are abstract classes, you do not need to explicitly declare the method as being pure virtual by appending = 0 to the method declaration.) -
In addition to methods, interfaces can define properties, indexers, and events, as shown here: -
interface IExampleInterface { // Example property declaration. int testProperty { get; } // Example event declaration. event testEvent Changed; // Example indexer declaration. string this[int index] { get; set; } }