Interfaces

Names are conventionally prepended w/ I (IMyInterface)

  • Parts of an Interface:
    • The Interface
      • Methods here have no body & are neither public nor private.
      • This is just the point of Creation.
    • Implementer
      • Class that implements the interface.
      • Detail is added here, in the emplementing class.
    • Client - Class that calls methods on the implementer
//  The Interface
public interface IStorable {
    void Read(string fileName);
    void Write(string fileName);
}


//  The Implementing Class
public class Document : IStorable {

    public void Read(string fileName) {
        Console.WriteLine($"Reading {fileName} into this document");
    }

    public void Write(string fileName) {
        Console.WriteLine($"Writing {fileName} to disk...");
    }
}


//  The Client calling the Interface Methods
public class SomeOtherClass {
    public void SomeMethod() {
        IStorable document = new Document();
        document.Read("myFile");
        //...
        document.Write("myNewFile");
    }
}

results matching ""

    No results matching ""