[Mono-list] Mono/.Net: object comparison

Martin Hinks mhinks at gmail.com
Fri Jul 1 10:26:41 EDT 2005


>if (list.Contains(a))
>       System.Console.WriteLine("bad");
>     else
>       System.Console.WriteLine("good");
>  }

Julien states that

>ArrayList list = new ArrayList();
>
>list.Add(new MyClass());
>
>MyClass one = new MyClass();
>
>bool result = list.Contains(one); --> return false with mono, true with
>dot-net

I've attached Dmitry's code that shows the behaviour working exactly
the same on .NET 1.1 and on Mono 1.1.8.

Both output "good".

Julien - can you post more code (ie. an entire test case that shows
your problem) please?



On 7/1/05, Dmitry Kostenko <bis0n at mail.ru> wrote:
> Hello Julien,
> 
> I'm sorry but I could not reproduce this behaviour with .NET 1.1. Am I doing anyhing wrong?
> Here is the source code:
> ===
> using System.Collections;
> 
> public class A
> {
>   public string a = "a";
>   public string b = "b";
>   public string c = "c";
> }
> 
> public class Program
> {
>   public static void Main()
>   {
>      ArrayList list = new ArrayList();
>      list.Add(new A());
> 
>      A a = new A();
>      if (list.Contains(a))
>        System.Console.WriteLine("bad");
>      else
>        System.Console.WriteLine("good");
>   }
> }
> ===
> It outputs "good" for me on .NET 1.1, compiled with csc, which I suppose to be
> the correct behaviour.
> 
> I have been confused a lot when you mentioned that new A() == new A() or
> ArrayList.Contains(new A()) may return true for a clean class A
> (without overridden Equals(), GetHashCode(), operator == etc).
> 
> Do you have Equals(), GetHashCode() or operator == () overridden?
> 
> With best regards,
> Dmitry Kostenko
> 
> > ------------------------------
> >
> > Message: 3
> > Date: Thu, 30 Jun 2005 18:16:16 -0400
> > From: Julien Sobrier <julien at sobrier.net>
> > Subject: Re: [Mono-list] Mono/.Net: object comparison
> > To: mono-list at lists.ximian.com
> > Message-ID: <42C46F30.4080205 at sobrier.net>
> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> >
> > Sorry for the delay. I did some tests with 1.1.8, same problem.
> >
> > The code looks like this:
> >
> > ArrayList list = new ArrayList();
> >
> > list.Add(new MyClass());
> >
> > MyClass one = new MyClass();
> >
> > bool result = list.Contains(one); --> return false with mono, true with
> > dot-net
> >
> > I'll add this to bugzilla next week.
> >
> > Thank you
> >
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
> 


-- 
Martin Hinks
http://www.m-s-d.net
-------------- next part --------------
/*
 * Created by SharpDevelop.
 * User: Martin
 * Date: 01/07/2005
 * Time: 15:24
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections;

namespace TST
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			   ArrayList list = new ArrayList();
			     list.Add(new A());
			
			     A a = new A();
			     if (list.Contains(a))
			       System.Console.WriteLine("bad");
			     else
			       System.Console.WriteLine("good");
			  }
		}
	
	
	public class A
{
  public string a = "a";
  public string b = "b";
  public string c = "c";
}

}


More information about the Mono-list mailing list