C#
  Home arrow C# arrow Page 2 - Interface, IEnumerable and IEnumerator in ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Interface, IEnumerable and IEnumerator in C#
By: James Yang
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 78
    2002-05-15

    Table of Contents:
  • Interface, IEnumerable and IEnumerator in C#
  • Object Oriented Programming
  • Using interfaces
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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
    {
    get
    {
    ..code to return string
    }
    }


    for

    int Property{get;set;}

    it should look like this:

    int Property
    {
    get
    {
    .. code to return int
    }
    set
    {
    ..code to set Property value…
    }
    }


    and finally, for

    void Method();

    it should look something like this:

    void Method()
    {
    ..code
    }


    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)
    {
    ..code
    }

    More C# Articles
    More By James Yang


       · Nice article I got to say. Being an intermediate level developer I had a fair good...
     

    C# ARTICLES

    - 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#







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway