C#
  Home arrow C# arrow Page 2 - Behind the Scenes Look at C#: Indexers
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
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#

Behind the Scenes Look at C#: Indexers
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 26
    2005-08-10

    Table of Contents:
  • Behind the Scenes Look at C#: Indexers
  • C# Indexers Example (First Iteration)
  • Explanation of code
  • A Look at the MSIL Code

  • 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


    Behind the Scenes Look at C#: Indexers - C# Indexers Example (First Iteration)


    (Page 2 of 4 )

    Please copy the following code and compile it:

    using System;
    using System.Text;

    namespace indexers
    {
      class Class1
      {
        static void Main(string[] args)
        {
          Department dept = new Department();
          dept.Name = "IT";

          // creating the first Employee object
          Employee emp1 = new Employee();  
          emp1.FirstName = "Maria"; 
          emp1.LastName = "Jack";

          // creating the other employee objects
          Employee emp2 = new Employee();
          emp2.FirstName = "Mina";
          emp2.LastName = "Jack";

          Employee emp3 = new Employee();
          emp3.FirstName = "Mick";
          emp3.LastName = "Nelson";

          Employee emp4 = new Employee();
          emp4.FirstName = "John";
          emp4.LastName = "smith"; 

          Employee emp5 = new Employee();  
          emp5.FirstName = "Joly"; 
          emp5.LastName = "Mac";

          Employee emp6 = new Employee();  
          emp6.FirstName = "Steven";
          emp6.LastName = "Joe";

          // using the indexer
          dept[0] = emp1;
          dept[1] = emp2;
          dept[2] = emp3;
          dept[3] = emp4;
          dept[4] = emp5;
          dept[5] = emp6;

          Console.WriteLine(dept.ToString());
          Console.ReadLine(); 
        }
      }

      class Department
      {
        private string name;
        private Employee[] Employees = new Employee[6]; 
     
        public Employee this[int arg]
        { 
          get
          {
            return this.Employees[arg];
          }

          set
          {
            this.Employees[arg] = value;
          }
        }

        public string Name
        {
          get
          {
            return this.name;
          }

          set
          {
            this.name = value;
          }
        }

        public override string ToString()
        {
          StringBuilder temp = new StringBuilder();
          temp.Append("The department "+this.name+" contains"+
    " the following Employees\n");

          foreach(Employee emp in Employees)
          {
            temp.Append("\n" + emp.ToString()); 
          } 
          return temp.ToString(); 
        } 

      }

      class Employee
      {
        private string firstName;
        private string lastName;

        public string FirstName
        {   
          get
          {
            return this.firstName;
          }

          set
          {
            this.firstName = value;
          }
        }

        public string LastName
        {
          get
          {
            return this.lastName;
          }

          set
          { 
            this.lastName = value;
          } 
        }

        public override string ToString()
        {
          return this.firstName+" "+this.lastName;
        }

      }
    }

    Run the application to get the following result to the console window:

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT