Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 8. Metaprogramming, protocols, a... > Protocols instead of interfaces

8.1. Protocols instead of interfaces

Interfaces are used in C# to specify behavior of objects. For example, if a class implements the IDisposable interface, then you can provide a Dispose method to release resources used by your objects. .NET has a whole host of interfaces, and you can create new ones. If a class implements an interface, it provides a static target for the compiler to call whenever an operation provided by that interface is used in your code.

Note

C# does have one example of duck typing: enumeration with the IEnumerable interface. To support iteration over an object,[1] classes can either implement IEnumerable or provide a GetEnumerator method. GetEnumerator can either return a type that implements IEnumerator or one that declares all the methods defined in IEnumerator.

[1] From http://msdn2.microsoft.com/en-us/library/ttw7t8t6(VS.71).aspx.



  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial