What Is an Indexer?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i want to know waht is an index and how we use it with a simple example
including the main of the program .

thanx
 
SemSem said:
i want to know waht is an index and how we use it with a simple
example including the main of the program .

An indexer is a pseudo-property that allows an object to be accessed like an
array.

<code>
using System;

public class Example
{
int this[int index]
{
get { return index; }
set { Console.WriteLine("Set: {0}",value); }
}

static void Main()
{
Example e = new Example();
Console.WriteLine("e[5]={0}",e[5]);
e[6] = 42;
}
}

</code>

This is, of course, a completely contrived and useless example, but it
illustrates how, in Main(), the object e is accessed as if it were an array
of integers. Note that the getter and setter for the indexer need not
implement behavior that's even remotely like that of an array, although
generally it's a good idea to only use an indexer for truly array-like
behavior.

Note that an object can have any number of indexers, and that the "indexes"
of the pseudo-array can have any type and there can be any number of them:

<code>
using System;

public class Example2
{
string this[string s1, string s2, string s3]
{
get { return string.Format("{0}:{1}:{2}",s1,s2,s3); }
set { Console.WriteLine("{0}:{1}:{2}={3}",s1,s2,s3,value); }
}

static void Main()
{
Example2 e2 = new Example2();
Console.WriteLine("{0}",e2["one","two","three"]);
e2["hello","world","!!"] = "Wow!";
}
}


</code>

-cd
 
Wow, you are so helpful. You really are the type of person that should be
responding to questions posted here.

Thanks for all your invaluable help!
 
Isn't is more to allow one data value to be treated like an array? In other
words, the bit representation of a given number may be 01100110000011111,
but instead of getting the Int16 value of this bit stream, you could treat
this like a Boolean array of 16 values?

How would/could you use this with traditional objects rather than data
types?


Carl Daniel said:
SemSem said:
i want to know waht is an index and how we use it with a simple
example including the main of the program .

An indexer is a pseudo-property that allows an object to be accessed like
an array.

<code>
using System;

public class Example
{
int this[int index]
{
get { return index; }
set { Console.WriteLine("Set: {0}",value); }
}

static void Main()
{
Example e = new Example();
Console.WriteLine("e[5]={0}",e[5]);
e[6] = 42;
}
}

</code>

This is, of course, a completely contrived and useless example, but it
illustrates how, in Main(), the object e is accessed as if it were an
array of integers. Note that the getter and setter for the indexer need
not implement behavior that's even remotely like that of an array,
although generally it's a good idea to only use an indexer for truly
array-like behavior.

Note that an object can have any number of indexers, and that the
"indexes" of the pseudo-array can have any type and there can be any
number of them:

<code>
using System;

public class Example2
{
string this[string s1, string s2, string s3]
{
get { return string.Format("{0}:{1}:{2}",s1,s2,s3); }
set { Console.WriteLine("{0}:{1}:{2}={3}",s1,s2,s3,value); }
}

static void Main()
{
Example2 e2 = new Example2();
Console.WriteLine("{0}",e2["one","two","three"]);
e2["hello","world","!!"] = "Wow!";
}
}


</code>

-cd
 
Scott said:
Isn't is more to allow one data value to be treated like an array? In
other words, the bit representation of a given number may be
01100110000011111, but instead of getting the Int16 value of this bit
stream, you could treat this like a Boolean array of 16 values?

You could use an indexer for that, sure. But then consider a
Dictionary<string,int>. The indexer makes this type behave as-if it were an
array of integers indexed by strings (the inverse of string[] in other
words).
How would/could you use this with traditional objects rather than data
types?

In your conceptual framework, what distinguishes a "data type" from a
"traditional object"?

Indexes can be defined on any class or struct, regardless of the intended
purpose. I certainly wouldn't recommend using an indexer on an object that
doesn't have some sort of array-like, or map-like feel to it, but there's an
awful lot of possibilities within those restrictions.

-cd
 
In your conceptual framework, what distinguishes a "data type" from a
"traditional object"?

Struct vs. Class
Indexes can be defined on any class or struct, regardless of the intended
purpose. I certainly wouldn't recommend using an indexer on an object
that doesn't have some sort of array-like, or map-like feel to it, but
there's an awful lot of possibilities within those restrictions.

So, how could an indexer help me with, say a CheckBox class, or a
DataAdapter class?

Thanks,

Scott
 
Scott M. said:
Struct vs. Class

That's a dangerous way of looking at it - in C# terms at least. "Data
type" vs "Traditional object" doesn't in any way capture "value type"
vs "reference type" IMO, and that's the crux of the difference between
structs and classes.
 
Jon Skeet said:
That's a dangerous way of looking at it - in C# terms at least. "Data
type" vs "Traditional object" doesn't in any way capture "value type"
vs "reference type" IMO, and that's the crux of the difference between
structs and classes.

To each his own. Looking at things as either a Data Type (Structure / Value
Type) or Traditional Object (Class / Reference Type), works fine for me
because I understand the difference between a ref type and a val type.

Your response doesn't answer my question though.
 
Scott M. said:
To each his own. Looking at things as either a Data Type (Structure / Value
Type) or Traditional Object (Class / Reference Type), works fine for me
because I understand the difference between a ref type and a val type.

What does a C++ class count as then? C++ classes aren't reference types
(in themselves - obviously you can use pointers), but surely they count
as "traditional objects" don't they? The difference between a struct
and a class is well documented - could you either give a clearer
definition of what a "data type" is compared with a "traditional
object" or link to an online resource with such a definition?
Your response doesn't answer my question though.

No, but I'm not entirely sure what your question really means. A
Hashtable is a class, and thus a "traditional object" by your
definition - I hope you see where Hashtable's indexer is useful...

Indexers are usually most commonly used in collections, but I don't see
that the struct vs class distinction is relevant to that, and indeed
I've rarely if ever seen a collection which is a value type in .NET.
 
What does a C++ class count as then? C++ classes aren't reference types
(in themselves - obviously you can use pointers), but surely they count
as "traditional objects" don't they?

I don't know, nor do I care since I don't use C++ in any way.
The difference between a struct
and a class is well documented - could you either give a clearer
definition of what a "data type" is compared with a "traditional
object" or link to an online resource with such a definition?

I just did in my last post:

Data Type (Structure / Value Type)
Traditional Object (Class / Reference Type)

I see nothing ambiguous or unclear with this.
No, but I'm not entirely sure what your question really means. A
Hashtable is a class, and thus a "traditional object" by your
definition - I hope you see where Hashtable's indexer is useful...

I think you are getting too hung up on minutia. The thread is about
indexers. My question was about a CheckBox class, or a
DataAdapter class. Just because what I call something isn't what you would
call it is irrelevant to the question. It is apparent that you understand
what my terminology means, so could you just get to the point?

I do see how an index is useful with a Hashtable or any other collection
class, but is this index passed to a Hashtable the same thing as an indexer
in this case? And if so, could you explain how?

Thanks.
 
Scott said:
I do see how an index is useful with a Hashtable or any other collection
class, but is this index passed to a Hashtable the same thing as an indexer
in this case?

Your question is a little malformed, but essentially, yes. An indexer
is a member, very similar to a property, that takes an index as an
argument. It's declared using the 'this' keyword. Hashtable has an
indexer, and you can create them on any of your classes or structs.
Here is a possible Hashtable indexer implementation:

public object this [Object key] {
get {
object o = Find(key);
return o;
}
set {
Put(key, value);
}
}

When you call
object o = hash["foo"]
you are calling the Hashtable indexer.

MSDN is your friend.
http://msdn.microsoft.com/library/en-us/csref/html/vclrfIndexedPropertiesPG.asp
 
Your question is a little malformed, but essentially, yes. An indexer
is a member, very similar to a property, that takes an index as an
argument. It's declared using the 'this' keyword. Hashtable has an
indexer, and you can create them on any of your classes or structs.
Here is a possible Hashtable indexer implementation:

This might also help: indexers can be applied to any class (or struct,
or interface), not just collection classes. For instance:

public string this[int index]
{
get
{
if (index < 12)
return "Good morning!";
else if (index < 18)
return "Good afternoon!";
else
return "Good evening!";
}
//...
}

Doesn't mean you *should* use it like this, mind.
 
Scott M. said:
I just did in my last post:

Data Type (Structure / Value Type)
Traditional Object (Class / Reference Type)

I see nothing ambiguous or unclear with this.

Okay, we'll go along with it. You should be aware that it's terminology
which is far from standard though. (I haven't seen anyone else use it.)
I think you are getting too hung up on minutia. The thread is about
indexers. My question was about a CheckBox class, or a
DataAdapter class. Just because what I call something isn't what you would
call it is irrelevant to the question. It is apparent that you understand
what my terminology means, so could you just get to the point?

Sure. You asked whether indexers would/could be used with "traditional
objects" (i.e. reference types). I've given an example of where
indexers *are* used in reference types.
I do see how an index is useful with a Hashtable or any other collection
class, but is this index passed to a Hashtable the same thing as an indexer
in this case? And if so, could you explain how?

The index passed to the Hashtable's indexer isn't the indexer itself -
the index is just the value of the argument. In the case of Hashtable,
that's a reference, so if you meant the index rather than the indexer,
it still provides an example. For instance:

Hashtable x = new Hashtable();
x["hello"] = "there";

The indexer is the member (or rather, the pair of members) which allows
you to use the [] syntax. There's nothing special about the value in
the brackets being a reference.
 
Okay, we'll go along with it. You should be aware that it's terminology
which is far from standard though. (I haven't seen anyone else use it.)

You should be aware that there *are* people who use this terminology and,
allthough it may not be standard jargon in your travels, it is in mine. In
fact, the separation of an object from a data type was standard operating
procedure in the VB 6.0 world. In that universe, a Boolean (for example)
was not treated as an object, it was treated as a data type. VB 6.0 even
went so far as to have a special keyword (Set) to differentiate between
referring to data types vs. objects.

As I said before, I think you should let the fact that I didn't phrase my
question using the verbiage you would have used go. I get the feeling that
you knew what I meant all along.
Sure. You asked whether indexers would/could be used with "traditional
objects" (i.e. reference types). I've given an example of where
indexers *are* used in reference types.

My question was:

"So, how could an indexer help me with, say a CheckBox class, or a
DataAdapter class?"


I do see how an index is useful with a Hashtable or any other collection
class, but is this index passed to a Hashtable the same thing as an
indexer
in this case? And if so, could you explain how?

The index passed to the Hashtable's indexer isn't the indexer itself -
the index is just the value of the argument. In the case of Hashtable,
that's a reference, so if you meant the index rather than the indexer,
it still provides an example. For instance:

Hashtable x = new Hashtable();
x["hello"] = "there";

The indexer is the member (or rather, the pair of members) which allows
you to use the [] syntax. There's nothing special about the value in
the brackets being a reference.
 
Scott M. said:
You should be aware that there *are* people who use this terminology and,
allthough it may not be standard jargon in your travels, it is in mine. In
fact, the separation of an object from a data type was standard operating
procedure in the VB 6.0 world. In that universe, a Boolean (for example)
was not treated as an object, it was treated as a data type. VB 6.0 even
went so far as to have a special keyword (Set) to differentiate between
referring to data types vs. objects.

As you pointed out that C# isn't C++, the same should be said here - C#
isn't VB 6.0, and it certainly isn't C# terminology, nor do I see that
it's useful terminology. Indeed, we've already seen that it causes
confusion.
As I said before, I think you should let the fact that I didn't phrase my
question using the verbiage you would have used go. I get the feeling that
you knew what I meant all along.

I really, really didn't. (And neither did Carl, clearly.)
My question was:

"So, how could an indexer help me with, say a CheckBox class, or a
DataAdapter class?"

And it doesn't, particularly. The same is true with overloading the ==
operator, which doesn't mean it's not useful in other classes. String
is a good example of another class which has an indexer - the indexer
returns the character at the given index, eg "hello"[3]=='l'.

It doesn't make sense to have indexers on *all* classes, but there are
plenty where it does make sense. Anything where it makes sense to ask
what the nth "element" (whatever that means for that class) would make
sense in terms of an integer indexer, and wherever there's a map
concept it may well make sense to index on other types such as strings.
 
It doesn't make sense to have indexers on *all* classes, but there are
plenty where it does make sense. Anything where it makes sense to ask
what the nth "element" (whatever that means for that class) would make
sense in terms of an integer indexer, and wherever there's a map
concept it may well make sense to index on other types such as strings.

Thanks. That's all I was really looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top