Click here to Skip to main content
15,908,437 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
C#
class First
    {
        string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public First(string _name)
        {
            
            name = _name;
        }
        public override int GetHashCode()
        {
            return name.GetHashCode();
        }
    }
    class Second
    {
        int id;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public Second(int _id)
        {
            id = _id;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            First obj1 = new First("mohamed");
            First obj2 = new First("Ali");
            Second Nobj = new Second(1);
            Hashtable ht = new Hashtable();
            ht[obj1] = Nobj;
            ht[obj2]=Nobj;
            ht.Add(new First("mohamed"), Nobj);           
        }
    }


why i dont get exception when i enter value "mohmaed" twice ??
Posted
Updated 12-Oct-12 15:21pm
v2
Comments
[no name] 12-Oct-12 21:25pm    
What exception do you think you should get? Why should you get that exception?
Anderso0on 12-Oct-12 21:31pm    
cause i have two value Similar ,hashtable dont allow enter two Reference similar but i override this option to name , if i enter two name similer i get exception

[no name] 12-Oct-12 21:34pm    
The objects used as keys by a Hashtable are required to override the Object.GetHashCode method (or the IHashCodeProvider interface) and the Object.Equals method (or the IComparer interface). The implementation of both methods and interfaces must handle case sensitivity the same way; otherwise, the Hashtable might behave incorrectly.
Anderso0on 12-Oct-12 21:36pm    
are you can help me to write the correct code??

Hashtables use the GetHashCode() method on objects.

The First class is being created twice and the the GetHashCode() method will return 2 different values (because there are two different objects in memory, even when the names are the same).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Oct-12 15:44pm    
Apparently, my 5. OP is totally lost. I would recommend to read on very basics, references, reference vs. value type, etc., and only then on hash codes.
--SA
Mehdi Gholam 17-Oct-12 16:34pm    
Thanks Sergey!
Sergey Alexandrovich Kryukov 17-Oct-12 15:49pm    
I also added related advice on not using HashTable as non-generic -- please see my solution.
--SA
[In addition to a correct answer by Mehdi, Solution 1:]

Also, you should understand that the class System.Collections.Hashtable was rendered obsolete as early as in .NET Framework, v.2.0, when generics were introduced. Instead, for all new development you should use the classes System.Collections.Generic.Dictionary, System.Collections.Generic.SortedDictionary or System.Collections.Generic.SortedList; please see:
http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].

Each of these classes can replace Hashtable, the differences are mostly different trade-off between data redundancy and speed of search algorithms.

—SA
 
Share this answer
 
Comments
Mehdi Gholam 17-Oct-12 16:35pm    
5'ed
Sergey Alexandrovich Kryukov 17-Oct-12 16:41pm    
Thank you, Mehdi.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900