[Mono-devel-list] Code that works in Mono - but not in .NET...

Ben Maurer 05mauben at hawken.edu
Fri Feb 27 07:54:21 EST 2004


Hello,

This is a very interesting bug. The behavior of Microsoft is actually pretty logical. The code would really have to look like this:

foreach (object obj in InnerList)
{
    if (value == null && obj == null)
        // found

    if (obj == null)
        continue;
    if (obj.Equals(value))
    {
        ....
    }
}

Which is a pain.

I filed this as http://bugzilla.ximian.com/show_bug.cgi?id=54927

-- ben
>>> Tom Shelton <tom at mtogden.com> 02/27/04 01:36 AM >>>
I have a case of some code I wrote using Mono, that doesn't work properly 
under .NET.  I thought maybe you guys would like to know about it, as it 
seems to be a difference in implementation of the ArrayList class...

Basically, what I have created is a typed collection class that inherits from 
System.Collections.CollectionBase.   The type that is stored in this 
collection has overriden Object.ToEquals to look something like this:

public class Column
{
	.....

	public override bool ToEquals(object obj)
	{
		if (obj is Column)
		{
			return (obj.ToString() == this.ToString());
		}
		else if (obj is string)
		{
			return ((string) obj == this.ToString());
		}
		else
		{
			return false;
		}
	}

}

In my ColumnCollection class,  I was implementing overloaded versions of the 
indexer, Contains, IndexOf, Remove, etc. that can take either another Column, 
or a string.  This works beautifully under Mono...

public class CollumnCollection : System.Collections.CollectionBase
{
	....

	public int IndexOf (Column value)
	{
		return this.InnerList.IndexOf(value);
	}

	public int IndexOf (string value)
	{
		return this.InnerList.IndexOf(value);
	}

	etc...
}

When, I would do
 ColumnCollection columns = new ColumnCollection();

columns.Add(new Column("TABLE", "FIELD"));
....

Column col = new Column("TABLE", "FIELD");
Console.WriteLine(columns.Contains(col));
Console.WriteLine("TABLE.FIELD");

the output, would be:
True
True

This does not work on .NET - what I get is:
True
False

In stepping through the code this morning, I found that in .NET my .Equals 
method is never called.  What I believe (no, I didn't look at the IL - I 
guess I should of), is that .NET is doing a sequence something like this when 
you call Contains, IndexOf, etc... (value is the object your looking for...)

foreach (object obj in InnerList)
{
	if (value.Equals(obj))
	{
		.....
	}
}

Where, as Mono seems to be doing something like this (and I think this is the 
more sensible approach by the way...)

foreach (object obj in InnerList)
{
	if (obj.Equals(value))
	{
		....
	}
}

I fixed these string methods easily enough - I just did my own loop, rather 
then delegate to the InnerList.  But, I thought I would point out this simple 
little inconsistency.

Tom
_______________________________________________
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list




More information about the Mono-devel-list mailing list