Home arrow C# arrow Page 2 - Interface, IEnumerable and IEnumerator in C#

Interface, IEnumerable and IEnumerator in C#

The concept of an interface is an important element in object-oriented programming. It's what makes OOP so structured but flexible at the same time. In this article James shows us what an interface is and also shows us how to implement some pre-defined interfaces using C#.

Author Info:
By: James Yang
Rating: 3 stars3 stars3 stars3 stars3 stars / 154
May 15, 2002
  1. · Interface, IEnumerable and IEnumerator in C#
  2. · Object Oriented Programming
  3. · Using interfaces
  4. · Conclusion

print this article

Interface, IEnumerable and IEnumerator in C# - Object Oriented Programming
(Page 2 of 4 )

For those people who have just begun learning about object oriented programming, you will have noticed how objects (such as instances of classes) work with each other without any flaws. How? The answer is, "through the inheritance features in OOP". Within this inheritance feature, there is a thing called an interface, which is what this article is all about.

Inheritance in OOP achieves two significant things. One is code reusability and second is the relationships between objects. By extending, not rewriting, the class written before, the amount of code written by developers is reduced considerably. Also, by establishing the relationship between objects, many things become possible in programming, for example, it is possible to polymorph one object into another.

Interface affects both the reusability and the relationship between objects.

What is an interface?
"An interface is a collection of method definitions (without implementations) and constant values".

Its purpose is to captures similarities between unrelated classes without forcing a class relationship, and to do this, an interface operates like a contract in the form of a collection of methods and constant declarations. When a class implements an interface, it promises to implement all of the methods declared in that interface.

A class can inherit multiple interfaces at once, and information on what interfaces are inherited by what class can be accessed by the framework and other objects. Hence, an object can identify an object that inherited a specified interface.

This information can be used by a particular framework or another object to determine whether the object is compatible with different objects or statements.

IEnumerable and IEnumerator are the best interface examples there are for you to understand the role of interface. Classes implementing these two interfaces are classified as an enumerable collection. This means that class behaves like any other collection object written by Microsoft, such as an ArrayList.

There are many features that define an enumerable collection, and by implementing these two interfaces, the framework guarantees that this object has these features. For example, an enumerable collection must have a method called MoveNext, which moves the cursor within the collection one step forward. By implementing the IEnumerator interface, the class is promising (and is required) to implement this method.

As mentioned above, when a class implements these interfaces and is defined as an enumerable collection, it behaves like one as well. This means that it is capable of being iterated using a foreach statement, just like we can do with an ArrayList object.

Why do we use interfaces?
We use interfaces because they allow reusability of code as well as help create useful relationships between objects.

Because your object is now able to work with other objects, you no longer have to other dependent objects that are only compatible with your objects. For example, if you are to create a new connection object like a mySql Connection object, then by implementing the right connection interfaces it will be able to work with the existing data adapter object and a data set object. If interfaces are not implemented, data adapter and data set objects won't be able to see if your object is compatible and that it implements the required methods, hence it won't be compatible with the data adapter nor data set objects, forcing you to write custom objects that implement the same functionality as the data adapter and data set objects. Here, we are creating relationships between unknown objects.

Creating interfaces
An interface is very easy to create. It's often the planning of how you can use interfaces in your application that takes time, not the actual creation of it, as it implements no logic.

Here's an example of a simple interface:

interface ISomething
// Read only property
string ReadOnlyProperty{get;}

// Property
int Property{get;set;}

// Method
void Method();

As you can see, an interface is created using the interface keyword. One thing to note here is that Microsoft STRONGLY recommends that ALL interfaces begin with the character 'I'. So in the example above, I've named my interface ISomething.

The interface shown above implements 1 read only property, 1 normal (read and write) property, and finally, 1 method that returns void. If a class is to implement this interface, then it must implement those 2 properties and 1 method to successfully compile.

To implement these members into a class, you must have the same name and signature for the members in the class where it will be implemented. So, for

string ReadOnlyProperty{get;}

your class's property should look something like

string ReadOnlyProperty
..code to return string


int Property{get;set;}

it should look like this:

int Property
.. code to return int
..code to set Property value…

and finally, for

void Method();

it should look something like this:

void Method()

You can also have methods that take one or more parameters. This is done like this:

void Method2(string, int);

The method shown above can be implemented by a class like this:

void Method2(string astring, in aint)

blog comments powered by Disqus

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials