Implementing an interface
Context: A class or struct implements an interface by providing concrete implementations for all its members.
Syntax
Section titled “Syntax”class ClassName : InterfaceName{ // implement all interface members}Example
Section titled “Example”public interface IPerson{ string Name { get; set; } void Introduce();}
public class Student : IPerson{ public string Name { get; set; } public void Introduce() => Console.WriteLine($"I'm {Name}, a student");}Multiple Interfaces
Section titled “Multiple Interfaces”public class FileManager : IReadable, IWritable{ public string Read() => "data"; public void Write(string data) { /* ... */ }}Interface as a Type
Section titled “Interface as a Type”You can use an interface type to hold any implementing object.
IPerson person = new Student { Name = "Alice" };person.Introduce(); // works