Introduction to C# part 3 : indexer

Hi everybody, it's a long time for me to continue the next part of basic C#.

Today, I will talk about indexers. What are the indexers?

Indexers're properties with the outward appearance of array. They allow you to access an object as an array.

You can use indexers if your types contain a collection as a member, indexers will helpful to you to access to the

internal collection in the object conveniently and safely.

Indexers're similar to properties that define a set and get method, but indexers will use this reference and indexers're nameless properties[you will see from an example].

 

This is an example code of indexer tester.

namespace IndexerTester
{
    class Program
    {
        static void Main(string[] args)
        {
            People people = new People();
            Console.WriteLine(people[1]);
            Console.WriteLine(people[0]);
            Console.ReadLine();
        }
    }
    public class People
    {
        private ArrayList people;
        public People()
        {
            people = new ArrayList();
            people.Add(new Person("A"));
            people.Add(new Person("B"));
            people.Add(new Person("C"));
        }
        public String this[int i]
        {
            get
            {
                Person p = people[i] as Person;
                return p.FullName;
            }
            set
            {
                Person p = people[i] as Person;
                p.FullName = value;
            }
        }
        // overloaded indexer
        public int this[String str]
        {
            get
            {
                int i = 0;
                foreach (Person p in people)
                {
                    if(p.FullName.Equals(str,StringComparison.OrdinalIgnoreCase))
                        return i;
                    i++;
                }
                return -1;
            }
        }
    }
    public class Person
    {
        public String FullName { get; set; }
        public Person(String str)
        {
            FullName = str;
        }
    }
}

You can see that there're 2 indexers in this code

public String this[int i]
{
            get
            {
                Person p = people[i] as Person;
                return p.FullName;
            }
            set
            {
                Person p = people[i] as Person;
                p.FullName = value;
            }
}

and...

public int this[String str]
{
        get
            {
                int i = 0;
                foreach (Person p in people)
                {
                    if(p.FullName.Equals(str,StringComparison.OrdinalIgnoreCase))
                        return i;
                    i++;
                }
                return -1;
            }
        }
}

Now, you will see from the code that you can access the collection in your object by (Object)[(index)], and

you can modify value by (Object)[(index)] = (Data).

 

This is a format of indexers -> public (Type) this[(Type t)] {...}

In the block, there are get or set accessors as properties.

 

^^ it's very easy right? If you have something to ask about C# or you want me to explain more about basic topics in very details you can post on this site.

I will try my best for your answer as much as I can ^^. CU Sooon for a next topic[you can request to me].

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (8) | RSS


Comments

June 17. 2009 18:25

iFourth

Nice entry ,Q =)

I think I will ask you again in Computer Network project lol
Can you write about how C# connect to the network entry?, I think it will be the good article for beginning the project ^^"

keep doing =)

iFourth

July 23. 2009 07:08

Hoodia

Thanks.. Funny, I actually had this on my mind a few days ago..

Hoodia

August 16. 2009 09:41

San Diego Web Design

Just what I was googling for! Thanks for sharing!

San Diego Web Design

August 16. 2009 09:41

Harley Davidson Motorcycles

I hadn�t thought of it before, but it�s definitely an interesting idea. Thanks for the insight.

Harley Davidson Motorcycles

August 20. 2009 18:19

darkkung

Sorry, I didn't come to this blog for a long time because I had to prepare for
ACM-ICPC in Phuket [Thailand] at 9-12 Aug. Now, I have a time to write a new article^^".

darkkung

September 20. 2009 14:59

fast loans

thanks !!  very instrumental post!

fast loans

September 28. 2009 20:26

bad credit loans

Hello. Great job. I did not expect this on a Wednesday. This is a good story. Thanks!

bad credit loans

October 27. 2009 07:21

ATV San Diego

You make us look like we�re smarter than we really are :o).

ATV San Diego

Add comment




biuquote
  • Comment
  • Preview
Loading



 

Tag Cloud