liveBook · Manning

Interface

As explained in Fabulous Adventures in Data Structures and Algorithms

An interface is a C# contract that specifies members a type can implement.

An interface is a C# contract that specifies the members a type can implement. For an abstract data type, it turns user needs into a precise boundary: legal operations, observable effects, and required behavior come before the data structure. This separation lets an implementation change while clients depend on the same operations. section 2.2 section 3.1

Design an interface by deciding what clients need before choosing how the type stores its data:

  • Name the values and operations that are legal, such as pushing, peeking, popping, and checking whether a collection is empty. section 2.2
  • Represent immutable updates with operations that return a new interface value instead of modifying the existing object. section 2.2 section 3.1
  • Expose enumeration when the abstraction is a sequence, and choose its natural direction explicitly. section 2.2 section 3.1
  • Evaluate the implementation afterward, including whether the promised operations remain cheap regardless of collection size. section 2.2 section 3.1

Stack and deque contracts

A stack interface can inherit IEnumerable<T> and declare Push, Peek, Pop, and IsEmpty. A deque extends the idea to both ends with PushLeft, PushRight, PopLeft, PopRight, Left, Right, and IsEmpty. Implementations can then provide immutable stacks or deques behind these contracts.

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Develops operation sets and implementation consequences, stopping short of general interface theory beyond immutable stacks and deques.
Examples
Uses generic immutable stack and deque contracts with push, pop, peek, end-access, emptiness, and enumeration operations.
What you can do after
Build it from scratch by specifying C# collection contracts and implementing immutable stack or deque types behind them.
How it connects
Connects interfaces with abstract data types, immutable data structures, stacks, queues, deques, generics, and IEnumerable<T>.

Also explained in

Compare how 7 books explain interface →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.