Wamiq,
By default, the hash of value types is based on the contents of the
structure, so creating a new structure, setting the values to the same
thing, and then using that as a hash will give you what you wanted.
However, for reference types, by default, the hashcode is based on the
reference equality, which is why you are seeing what you are seeing.
You have some options here. First, you can provide your own hash code
provider, and not use the GetHashCode implementation on the Label class
(which defers to Object.GetHashCode anyways), or, you could create a
structure that has the properties of the label you want to use as your key.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Wamiq Ansari" <wa***@msn.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Hi,
I am using a System.Windows.Forms.Label object as key in my Hashtable. One
thing that has surprised me is that if I create two Label objects with
same properties (ie. Name, TabIndex,Text,Size,Dock) and use them as Key in my
Hashtable it doesn't give any error.
I thought, generally, in a Hashtable we cannot use two same objects as
keys. The Label Objects in this case are being created in a loop and used as
keys in a Hashtable.
Can anyone please shed some light on this.
Regards,
Wamiq Ansari
wa***@msn.com