Sunday, July 15, 2018

Awesome Games by Darsh

Monday, July 5, 2010

Equality in C# – Part 2(GetHashCode)




In my previous post Equality in C# – Part 1, I had explained how .Equal function behaves in C#. Microsoft recommends if you are writing an own functionality of .Equal method you should implement .GetHashCode method as well. In fact, C# compiler will emit a warning message if you do not override GetHashCode method while overriding .Equal method.

The reason behind this is that implementation of System.Collections.Hashtable type and System.Collections.Generic.Dictionary requires that if any two objects are equal then they should return the same HasCode. If we are writing our own implementation of object equality you should implement .GetHasCode to ensure two equal objects should return same hash value.

Basically, when you add a Key/Value pair in Hashtable / Dictionary, first has code of the key object is obtained first. The integer has code will indicate which “bucket” key/value will be stored. If HasCode of the key is changed then object will become unsearchable in Hashtable / Dictionary. Let’s see one example:
using System;
using System.IO;
using System.Globalization;

public class Example
{
  public static void Main()
  {

    System.Collections.Hashtable table = new System.Collections.Hashtable();
    A obj1 = new A();   
    obj1.Name = "Vikas";

    table.Add(obj1, obj1.Name);// Here the Hascode of Obj1 is stored. 

    obj1.Name = "Changed "; // Here HasCode of obj1 will be changed due to our bad implementation

    Object name = table[obj1];// will return Null .      
 
  }
}
public class A : IEquatable<A>
 
{
  private String _name;
  public String Name
  {
    get
    {
      return _name;
    }
    set
    {
      _name = value;
    }
  }
  public override int GetHashCode()
  {
    return Name.GetHashCode();
  }
  #region IEquatable<A> Members

  public bool Equals(A other)
  {
    if (String.Compare(Name, other.Name, StringComparison.OrdinalIgnoreCase) == 0)
      return true;
    else
      return false;
  }

  #endregion
}

When defining Hash Code try to follow these guidelines:

1)Use an algorithm that gives good random distribution for best performance.
2)The fields used in the algorithm should be immutable. They should be initialized in the constructor of the class and should not be changed in the life cycle of the object.
3)Your algorithm should be executed very quickly.
4)Object with the same value should return same hash code.
5)You can use base.GetHashCode if you want.

Object class does not know anything about its derived class’s values so object’s GetHashCode implementation guarantee a uniquely identified number with in the app domain. Its also guarantee that the number will not be changed throughout object’s life time. If you want to return a unique number for your object you can use System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode function which takes an object as parameter.

System.ValueType’s implementation of GetHashCode function uses reflection so we should avoid using it because reflection is slow.

One last point you should not store HashCode of any object in your database directly until you have your own unique implementation of GetHashCode method. Do not believe in GetHasCode implementation of object class as it might differ in different version of CLR.

Thursday, July 1, 2010

Equality in C# – Part 1



In normal programming practice we always have a need to compare two variables. In C# we have two major choices for accomplishing this task

1) Using == operator

2) Using .Equals method.

When we use == operator it actually checks the references of objects and will return same result as object.ReferenceEquals, if there is no overloaded implementation for == exists for that type.

Lets talk about behavior of == and .Equals in different cases

A) Value type : (primitive type). All value type like int, double is inherited from “System.ValueType” class which overloads .Equals method. The overloaded implementation of .Equals method uses reflection to check equality which in turn very slows. == Operator in this case has default implementation which check the actual value store in the primitive type (as Value types are stored in stacks so their actual value will be compared).

B)String Type: implementation of String overloads both == and .Equal methods to ensure equality of content not identity.


String s1 = "test";
      String s2="test";

      //true ,true , true
      Console.WriteLine (String.Format("{0} {1} {2}",s1==s2,s1.Equals(s2), object.ReferenceEquals (s1,s2))); 

      // Now lets change their refrence.
      s1 = "1test".Substring(1);

      //output true ,true , false       
      Console.WriteLine(String.Format("{0} {1} {2}", s1 == s2, s1.Equals(s2), object.ReferenceEquals(s1, s2)));
 




C) Other Reference Type : For other reference types we need to give our own implantation of .Equals and == operator.

Rules to implement .Equal method

1) If you are planning to implement .Equal method then try to implement
through IEquatable interface to ensure type safety implementation.
2) Overload == and != operator methods to ensure same functionality
as .Equal overloaded method
3) Avoid calling base.Equals method.
4) Implement GetHasCode() method with .Equal method.

In next part I will explain why GetHashCode() method is necessary while implementing .Equals method.

Thursday, March 11, 2010

MVC Architectural Pattern ( Part 1) : Basic Concepts

I have implemented MVC (or batter to say many varition) of MVC in few of my projects. MVC provide us a very strong archtitecture but its current understanding is very importent. MVC should not be confused with Tree Tier architecture. MVC is different and its has lots more. Ok Let me start my explaination….


MVC is an architectural pattern as opposed to desgin pattern. Architectural pattern has wider scope as compare to desgin pattern. Most common architectural patterns are :



1) MVC


2) MVP


3) Multitier Architecture ( 3 tier )


4) Peer to peer.

MVC concent was indtroduced by SmallTalk’s inventors Trygve M. H. Reenskaug. MVC contain tree major components


1) Modal : Connected to user’s mind


2) View : Connected to user’s eyes


3) Controller: Connected to user’s hands.

In simple terms View is what we see ( using eyes J ). Modal is our data ( which is in our mind ) and controller is our hand ( which controls the input). You can think of MVC as a sepration of your data from display where your data is not aware where it will be rendered. In pratical software desgin




1) Modal is our object represeting data. Modal are SMART.


2) View is visualization of state of model. View are DUMBS

 
3) Controler facilate the change of state of model. It takes user input. Controller are THIN






In a typical ASP .Net application View are our ASPX pages ( purley DUMB who just render the data ). Controller are our code behind ( which handle user input ) and modal are out businnes object.

MVC Pasive Mode



In MVC, each View must have a controller. Noramally contrller facilate the comminucation between view and model. Whenever there is a change in data , controller takes the responsibility of notifiing the view that data has been changed and you need to update yourself. That is normally done using direct call to view. In this Modal is isloated and its not ware about View and controller. But this approach has a problem if I have a multiple disaplay of same data ??? Think you are going to a builder and he show you different view of your dream house. The Blacony view ,Front door view. In a way he is showing same data ( our dream house ) but different angle. If data changes then all view must react and should change themselves.


The Varition

According to MVC communiccation between Model and View can be done using direct call or using observer desgin pattern . As I love desgin pattern so I will take observer as an example. In this View need to register them with model through controller. Whenever any changes happen in data the Modal notifies all its registered view about the change. In the notificaion ( in .Net we can say events ) its usual to passed the view who actually made that data change. Remember all user inputs are handeled by the controller.


This was the basic intro of MVC, in Part 2 I will try to explain MVC using .Net winform and ASP.Net implemtation. In Part 3 we will discuss about architectural benefits of MVC.

Friday, March 5, 2010

Updating Text in TreeView Control (Fastest Way)

You might be wondering why i am writing blog for simplest task like Updating a Node text in Treeview Control ( Winform ) ?

Answer is performence !!!!
If you have a TreeView control with thousands or mllions of TreeNode inside and you want to change Text of a specific Node or set of Node then you might notice some performence trouble.( by the way What is the fastest way of finding the node in Tree View ? : Wait for my next blog... ;) ) .

Ok lets come to the point again : When we call function Node.Text TreeView tries to render itself and it takes time. So what is the solution ?

The solution is to calll TreeView.BeginUpdate before updating Text and call TreeView.EndUpdate() after the text change operation.

I have noticed performence increased 20 times with this method.

Thanks and have a good Weekend !

Friday, February 26, 2010

IEnumerable and LINQ inside



IEnumerable provide us an easy way to iterate through a collection but it actually does not hold that collection data by itself. It means whenever you will ask IEnumerable to get next data if will go to the source again. What if the source of IEnumerable collection is a LINQ query and content of that LINQ query keep on changes?

Yes our IEnumerable will be affected automatically. Here is one example :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      List<Person> ListOfPersons= new List<Person> {
      new Person { Salary =1000, Name ="Person1"},
      new Person { Salary =2000, Name ="Person2"}
      };
      Person Person3 = new Person { Salary =3000, Name ="Person3"};
      Person Person4 =  new Person { Salary =4000, Name ="Person4"};

      ListOfPersons.Add (Person3 );
      ListOfPersons.Add(Person4); 

      IEnumerable<Person> BadSalaryList = from person in ListOfPersons
                                           where person.Salary < 2500
                                           select person;

      // Now above LINQ will be Executed 
      Console.WriteLine(BadSalaryList.Count()); // output 2 

      //Lets change Person3 
      // This will re execute the query 
      Person3.Salary = 500; // This person will be automatically  included in BadSalaryList        


      Console.WriteLine(BadSalaryList.Count()); // output 3 Person3 Added in last 

      // Lets change salary of Person1    
      // This will re execute the query 
      ListOfPersons[0].Salary = 7000; 


      Console.WriteLine(BadSalaryList.Count()); // output 3 Person3 Added in last 
      
  //    Console.WriteLine(BadSalaryList.Count());
      Console.ReadLine();

    }
  }

  public class Person
  {
    public int Salary
    {
      get;
      set;
    }
    public String Name
    {
      get;
      set;
    }
    


  }
}



If we notice whenever we change the data of Person object IEnumerable collection tries to reevaluate LINQ based on new collection. We need to be very careful about this as IEnumerable collection is not fixed
1) It will be changed based on LINQ conditions
2) We might need to see performance as it will re evaluate the query on each change.

This feature is useful when you want to display list of Error objects to user and providing user a functionality to correct the error. In that case the collection will be automatically managed.

If we do not want collection to be modified then we can store output in List<> . Here is the example :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      List<Person> ListOfPersons= new List<Person> {
      new Person { Salary =1000, Name ="Person1"},
      new Person { Salary =2000, Name ="Person2"}
      };
      Person Person3 = new Person { Salary =3000, Name ="Person3"};
      Person Person4 =  new Person { Salary =4000, Name ="Person4"};

      ListOfPersons.Add (Person3 );
      ListOfPersons.Add(Person4); 

      List <Person> BadSalaryList = (from person in ListOfPersons
                                           where person.Salary < 2500
                                           select person).ToList ();

      // Now above LINQ will be Executed 
      // Only once 
      Console.WriteLine(BadSalaryList.Count()); // output 2 

      //Lets change Person3       
      //Query will not be executed 
      Person3.Salary = 500; 
      Console.WriteLine(BadSalaryList.Count()); // output 2

      // Lets change salary of Person1    
      //Query will not be executed 
      ListOfPersons[0].Salary = 7000;

      Console.WriteLine(BadSalaryList.Count()); // output 2 
      
  //    Console.WriteLine(BadSalaryList.Count());
      Console.ReadLine();

    }
  }

  public class Person
  {
    public int Salary
    {
      get;
      set;
    }
    public String Name
    {
      get;
      set;
    }
    


  }
}

Monday, February 22, 2010

Delegates Inside




Delegates are one of most complex and simplest concept in .Net ;) .

Delegates in c# are defained as Function Pointers. Those functions can be an instance function or a static function. In case of instance function delegeate store the reference of that object in its property called "Target".

Once an instance function is added to a deletegate then that function will always be invoked by delegate event if you set instance to null................

Lets take an example :

using System;

static class Program
{
[STAThread]
static void Main()
{

EventHandelerClass handeler = new EventHandelerClass("My Text");
delShow showMe = new delShow(handeler.OnShow);
handeler.Text = "Changed ";
handeler = null;  // set to null 
GC.Collect(); // Make sure it is released 
Console.WriteLine(showMe(" From Delegate"));
Console.ReadLine();

}
}


public delegate String delShow(String msg);

public class EventHandelerClass
{
public String Text
{
get;
set;
}
public EventHandelerClass(string text)
{
Text = text;
}

public String OnShow(String msg)
{
return this.Text + msg;
}


}



Above program will display message "My Text From Delegate" even if handeler is set to null.

This is because when you add a instance method to a delegate it store reference of that instance in its "Target" property. When we set handeler = null actually we are saying now "handeler" will no longer point to the address but delegate "showMe" is still pointing to same address ( ref How reference types are stored ? ).

Whole idea is once we have assign a function pointer to a delegate or event it will always call it ; simply setting instance to null will not remove the handler. To remove handler we need to either use "-=" or "MulticastDelegate.Remove" method.

I faced this problem while implementing MVP in one of my project where I have disposed a consumer form but the event handler was still called. in my next article I will try to explain MVP implementation in detail.