[Mono-list] System.Collections intricacies

Jonathan Pryor jonpryor@vt.edu
Sun, 01 Feb 2004 14:31:19 -0500


On Thu, 2004-01-29 at 14:36, Andrew P. Wilson wrote:
<snip/>
> However, I am finding that the indexer is not called at all and from
> disassembling the executable, the following instructions are occurring
> where I would have thought the indexer would have been called:

> IL_0099:  callvirt instance object class
> [mscorlib]'System.Collections.ArrayList'::'get_Item'(int32)
> IL_009e:  unbox [mscorlib]System.Int32

> Normally, this wouldn’t be a problem for me.  However, I cannot find
> this “get_Item” function anywhere in the mcs or mono source code. 
> There is also a corresponding “set_Item” function that is called as
> well, but again, I cannot find it in the source code.

What you've stumbled across is C# "name mangling".  In order to be
cross-language friendly (as not all languages support operator
overloading), all methods in IL are normal functions, but have a special
naming convention so that languages that support operator overloading
can view them.

For example, the addition operator:

	public static Foo operator+ (Foo lhs, Foo rhs);

would become:

	public static Foo op_Addition (Foo lhs, Foo rhs);

after it's generated.

The array index operator becomes get_Item for the getter, and set_Item
for the setter.  The "Item" part of the names can be changed by using
the System.Runtime.CompilerServices.IndexerNameAttribute (which is how
System.String has a Chars indexer in VB.NET).

This is also true of properties:

	public String MyProperty {
		get {return "Foo";}
		set {/* ignore */}
	}

becomes:

	public String get_MyProperty () {...}
	public void set_MyProperty (String value) {...}

> Is there a situation where the ArrayList Indexer will be called?  And
> do the get_Item and set_Item functions exist somewhere in the source
> code?

As seen above, the indexer is being called, you just need to know about
the name mangling.

 - Jon