[Mono-devel-list] Code that works in Mono - but not in .NET...
Tom Shelton
tom at mtogden.com
Fri Feb 27 01:35:13 EST 2004
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
More information about the Mono-devel-list
mailing list