Using C# Interfaces

Do you think C# interfaces are worth using, or are abstract classes more suitable? The truth is that both interfaces and abstract classes provide for powerful class hierarchies. Enterprise development and networking specialist Stephen B. Morris describes how to get off the ground quickly with C# interfaces.

Like this article? We recommend

C# and Java: Two Sides of the Same Coin?

The more I look at C# and Java, the more similarities I see. C#
is inextricably interwoven with the .NET platform, whereas Java is
platform-neutral. Like many great rivalries, both languages probably contribute
useful ideas to each other. Where both of these languages are similar is in the
system area. To dig into the system level, you tend to need C or C++, because
system-level programming (such as device drivers, memory management, and so on)
tends to live outside the virtual environment of managed languages.

However, before we predict the demise of the likes of C and C++,
we must remember that C is the development language of choice for many smaller
platforms. And so it goes: As new platforms are developed, the older languages
often are dusted off and used for developing applications. This multi-language
world is good news for developers, because the complexity is so great that we're
still needed—maybe now more than ever.

Should programmers specialize in C# or Java? I suggest a
flexible approach: Try to acquire excellence in one while maintaining knowledge
of the other. Obviously, your day-to-day work will dictate what language you
use, but in uncertain economic times it's good to be able to move from one
mainstream language to another.

With this goal in mind, let's examine the C# interface, which
bears a close resemblance to the Java interface. We'll start with a really
simple example and build on it throughout this article.

NOTE

All the code in this article was written using the venerable SharpDevelop IDE. The code is supplied as a downloadable source.zip file that can be extracted and dropped into a SharpDevelop solution. After unzipping the files, you should be able to build and use the supplied SharpDevelop solution directly. (As usual, I used SharpDevelop and just created a console application.) If you prefer to build the source code from scratch, you need to create a solution. Simply drop the one supplied source code file into your solution. Once this is done, the project should build and run successfully.