Overloading "Property Indexers" in C#

by kiranpvn kumar on 2/9/2012 8:41:31 PM
Rated 0 from 0 votes
Brief: The sample demonstrates on overloading a "Property Indexer" using C#.
Posted to: Basics of Object Oriented Programming in .NET
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

We have already seen various articles on "overloading" methodology earlier (check "Related Posts" section).  In this article, we are going to overload "Property Indexers" in C#.

The sample demonstrates the following:
  • How to define more than one Property Indexer using "Overloading"
  • How to define Property Indexer with no parameters (default)
  • How to define Property Indexer with (string as) parameter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace OverLoadingIndexer
{
    class Friends
    {
        private string[] names; // Private data member, its an array
        public Friends(int n)
        {
            names = new string[n];
            for (int i = 0; i < n; i++)
                names[i] = "empty";
        }
        public string this[int i]       //Its an indexer and looks like property definition
        {
            set
            {
                names[i] = value;
            }
            get
            {
                return names[i];
            }
        }
        public string this[string item] //Indexer Overlading with string in the place of index position and its a write only
        {
            set
            {
                for (int i = 0; i < names.Length; i++)
                {
                    if (names[i] == item)
                    {
                        names[i] = value;
                        break;
                    }
                }
            }
        }
        public void Show() // display the contents of array
        {
            for (int i = 0; i < names.Length; i++)
                Console.WriteLine(names[i]);
            Console.WriteLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Friends FriendsofJames = new Friends(5);
 
            //Following statements are used to assign different names to "FriendsofJames" Object
            FriendsofJames[0] = "Jagadish P";    // Object behaves like an array
            FriendsofJames[1] = "Kiran";
            FriendsofJames[2] = "Krishna";
            FriendsofJames[3] = "Prasad";
            FriendsofJames[4] = "Ravi Varma";
 
 
            Console.WriteLine("Friends list of James :");
            FriendsofJames.Show();
 
            //We could change/modify the content using friendname instead of index position
            FriendsofJames["Jagadish P"] = "Jagadeesh Pulakanandam"; // invokes indexer with string parameter, and corrects/modifies "Jagadish P" with "Jagadeesh Pulakandam"
            FriendsofJames["Kiran"] = "Kiran Kumar";
 
            Console.WriteLine("Friends list after update");
            FriendsofJames.Show();
 
        }
    }
}


Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following


Discussion
No comments yet!