Hands-On Network Programming with C# and .NET Core
上QQ阅读APP看书,第一时间看更新

The interface or abstract class

I've been describing the abstract WebRequest base class as providing an interface for how Microsoft intends developers to interact with network operations. However, I must admit that this isn't entirely accurate; WebRequest is, technically, an abstract class. For those readers who are unfamiliar with the distinction, it's actually quite trivial for our purposes.

Abstract classes do, in fact, define an interface for working with their implementations. The relevant distinction between the two is that, with an abstract class, any given method provided as part of the interface will typically have a default implementation defined within the abstract base class itself. So, the methods provided by an abstract class still define the interface through which you, as a consumer of the concrete classes, will interact with the implementations of the class. It's really just a distinction of where the obligation falls to define the expected behavior for that interface. Since you can't instantiate an abstract class any more than you can instantiate an interface definition, the difference is entirely trivial. Unless, of course, you choose to inherit from the WebRequest class yourself (which we will do at the end of this chapter). For now, though, let's just review the specification provided by WebRequest.