5 Replies - 858 Views - Last Post: 23 August 2010 - 08:29 AM Rate Topic: -----

#1 killcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 24-March 10

The use of C# Indexers

Posted 22 August 2010 - 04:38 AM

I am learning the OO features of the C# language.

As i know Properties are used to access private members of a class, "Indexers" are used too access private members of an array correct?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace indexers
{
    class Program
    {
        class myclass
        {
            private string[] data = new string[5];
            private int age = 16;
            private double speed = 3.0;
            // now the indexers
            public string this[int index]
            {
                get
                {
                    return data[index];
                }
                set
                {
                    data[index] = value;
                }
            }
            public int ageproperty
            {
                get 
                {
                    return(age);
                }
                set 
                {
                    age = value;
                }
            }
            public double speedproperty
            {
                get
                {
                    return speed;
                }
                set
                {
                    speed = value;
                }
            }
        }
        static void Main(string[] args)
        {
            myclass mc = new myclass();
            mc[0] = "rajesh";
            mc[1] = "blah";
            mc[2] = "deblah";
            mc[3] = "kloa";
            mc[4] = "loool";

            Console.WriteLine("NAMES: ");
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(mc[i]);
            }
            // print the ages
            Console.WriteLine("AGE: {0}",mc.ageproperty);
            Console.WriteLine("SPEED: {0}", mc.speedproperty);
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: The use of C# Indexers

#2 Guest_Guest*


Reputation:

Re: The use of C# Indexers

Posted 22 August 2010 - 04:50 AM

Something like that... you could even have a string indexer like so:

public string this[string key]

Was This Post Helpful? 0

#3 killcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 24-March 10

Re: The use of C# Indexers

Posted 22 August 2010 - 04:56 AM

View PostGuest, on 22 August 2010 - 03:50 AM, said:

Something like that... you could even have a string indexer like so:

public string this[string key]

of course :P
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5830
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: The use of C# Indexers

Posted 22 August 2010 - 07:18 AM

Your code seems ok. I don't like the hard coded size, though.

The point of an indexer is to simply make access to elements more intuitive. e.g. we can wrap a dictionary and make it easier.
class MyClass {
	private Dictionary<int, string> data = new Dictionary<int, string>();
	private int lastIndex = -1;
	
	public string this[int index] {
		get { return data.ContainsKey(index) ? data[index] ? null; }
		set {
			if (index>lastIndex) { lastIndex=index; }
			data[index] = value;
		}
	}
	
	public int AddValue(string s) {
		data[++lastIndex] = value;
		return lastIndex;
	}
	
	public int LastIndex { get { return lastIndex; } }

	static void Main(string[] args) {
	MyClass mc = new MyClass();
		mc[0] = "rajesh";
		mc[1] = "blah";
		mc[2] = "deblah";
		mc[3] = "kloa";
		mc.AddValue("loool");

		Console.WriteLine("NAMES: ");
		for (int i = 0; i <= mc.LastIndex; i++) {
			Console.WriteLine(mc[i]);
		}
	}
}


Was This Post Helpful? 2
  • +
  • -

#5 killcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 24-March 10

Re: The use of C# Indexers

Posted 22 August 2010 - 08:44 AM

View Postbaavgai, on 22 August 2010 - 06:18 AM, said:

Your code seems ok. I don't like the hard coded size, though.

The point of an indexer is to simply make access to elements more intuitive. e.g. we can wrap a dictionary and make it easier.
class MyClass {
	private Dictionary<int, string> data = new Dictionary<int, string>();
	private int lastIndex = -1;
	
	public string this[int index] {
		get { return data.ContainsKey(index) ? data[index] ? null; }
		set {
			if (index>lastIndex) { lastIndex=index; }
			data[index] = value;
		}
	}
	
	public int AddValue(string s) {
		data[++lastIndex] = value;
		return lastIndex;
	}
	
	public int LastIndex { get { return lastIndex; } }

	static void Main(string[] args) {
	MyClass mc = new MyClass();
		mc[0] = "rajesh";
		mc[1] = "blah";
		mc[2] = "deblah";
		mc[3] = "kloa";
		mc.AddValue("loool");

		Console.WriteLine("NAMES: ");
		for (int i = 0; i <= mc.LastIndex; i++) {
			Console.WriteLine(mc[i]);
		}
	}
}


Hmmmmm, Okay! Never came across "Dictionary" in C#, I'll look into it :P
Learn something everyday ^^
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4488
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: The use of C# Indexers

Posted 23 August 2010 - 08:29 AM

"Dictionary" is like a typesafe hashtable. It's a very useful class, if you need to pair data with a "key" value (which could be anything, not just a string or an int).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1