I know the answer (it's 42)

A blog on coding, .NET, .NET Compact Framework and life in general....

October, 2007

Posts
  • I know the answer (it's 42)

    One of the most interesting machine which I've ever seen - II

    • 3 Comments

    Last year I had posted about a a drum playing machine I had seen.

    This time I got a full video of it in action. Looks and sounds awesome

     

  • I know the answer (it's 42)

    Mutable objects and hashcode

    • 2 Comments

    Mutable objects can lead to situation wherein it can be put into a hash but never retrieved from it (or rather even worse something else gets returned)...

    Even though it is evident in most cases, but its not so evident all the time especially when we have deep hashcode dependencies. Consider the following

    class Person
    {
        public Person(string name, Person parent)
        {
            this.name = name;
            this.parent = parent;
        }
    
        public string name;
        public Person parent;
    
        public override int GetHashCode()
        {
            int hashCode = name.GetHashCode();
            if (parent != null)
                hashCode ^= parent.GetHashCode();
    
            return hashCode;
        }
    }
    
    static void Main(string[] args)
    {
        Dictionary hash = new Dictionary();
    
        Person parent = new Person("Beeblebox", null);
        Person zaphod = new Person("Zaphod", parent);
    
        hash.Add(zaphod, zaphod);
        parent.name = "SomethingElse";
    
        Console.WriteLine(hash.ContainsKey(zaphod)); // Will print false
    }

    Here Person is mutable on different levels. Its directly mutable and also deep-mutable as it contains a reference to parent (which in turn is mutable).

    Additionally, Person's hashcode depends on it's mutable fields. So in effect if any of the mutable references change then the next GetHashCode will return a different value and hence its lookup inside Dictionary will fail as Dictionary uses the hashcode of the objects place inside it.

    Since in the example above the mutation of parent happens inline, it's easy to figure it out. However, it could well be in some obscure corner of the code base.

  • I know the answer (it's 42)

    World Teacher's day

    • 1 Comments

    Today is world teacher's day, a day dedicated to some of the most important people in our lives. For me this is even more important because I come from a family of teachers including my father, grand mom and a lot of my uncles/aunts and even my wife.

    I wanted to take this opportunity to thank all my teachers who have helped me reach where I'm today.

Page 1 of 1 (3 items)