<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6783752066256544375</id><updated>2024-09-05T09:36:54.737+08:00</updated><category term="C#"/><category term="Delegate"/><category term="IEnumerable"/><category term="LINQ"/><category term="Performence"/><category term="TreeView"/><category term="Winform"/><title type='text'>Vikas Sharma</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://vikas-ksharma.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://vikas-ksharma.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>vikas</name><uri>http://www.blogger.com/profile/15473935587636791410</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHa3cok5b8XpydIRDonXD-s-ezQ9e2ABuzunQYtYQL1VH9Lcr4y_Fn229ipUP1tSaVK-132qsl8kRzP4mNwtyHqM7cJLFG9mNIzq0_YmKEJ_G-tOOnTt5VcSXKo-5uei8/s220/vikas.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6783752066256544375.post-441837382350822840</id><published>2018-07-15T00:29:00.002+08:00</published><updated>2018-07-15T00:41:12.755+08:00</updated><title type='text'>Awesome Games by Darsh</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;a href=&quot;http://www.coolmath-games.com/&quot;&gt;Cool Math Games by Darsh&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;a href=&quot;http://www.coolmath-games.com/&quot;&gt; &lt;/a&gt;


&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6; font-size: x-large;&quot;&gt;&lt;a href=&quot;http://kizi.com/games/red-ball-2&quot;&gt;Red Ball Games by Darsh&lt;/a&gt;
&lt;/span&gt;&lt;span style=&quot;background-color: #e06666;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: #e06666;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: #e06666;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: #e06666;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://vikas-ksharma.blogspot.com/feeds/441837382350822840/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6783752066256544375/441837382350822840?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/441837382350822840'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/441837382350822840'/><link rel='alternate' type='text/html' href='http://vikas-ksharma.blogspot.com/2018/07/awesome-games-by-darsh.html' title='Awesome Games by Darsh'/><author><name>vikas</name><uri>http://www.blogger.com/profile/15473935587636791410</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHa3cok5b8XpydIRDonXD-s-ezQ9e2ABuzunQYtYQL1VH9Lcr4y_Fn229ipUP1tSaVK-132qsl8kRzP4mNwtyHqM7cJLFG9mNIzq0_YmKEJ_G-tOOnTt5VcSXKo-5uei8/s220/vikas.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6783752066256544375.post-2424936678865940728</id><published>2010-07-05T11:41:00.001+08:00</published><updated>2010-07-05T11:41:26.876+08:00</updated><title type='text'>Equality in C# – Part 2(GetHashCode)</title><content type='html'>&lt;a href=&quot;http://anyurl.com/&quot; rel=&quot;tag&quot; style=&quot;display: none;&quot;&gt;CodeProject&lt;/a&gt;&lt;br /&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, &quot;Courier New&quot;, Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;&lt;br /&gt;
&lt;br /&gt;
In my previous post &lt;a href=&quot;http://vikas-ksharma.blogspot.com/2010/07/equality-in-c-part-1.html&quot;&gt;Equality in C# – Part 1&lt;/a&gt;, 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. &lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
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: &lt;br /&gt;
&lt;/HR&gt;&lt;pre class=&quot;csharpcode&quot; style=&quot;background-color: #dddddd;&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System;
&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System.IO;
&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System.Globalization;

&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; Example
{
  &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Main()
  {

    System.Collections.Hashtable table = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; System.Collections.Hashtable();
    A obj1 = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; A();   
    obj1.Name = &lt;span class=&quot;str&quot;&gt;&quot;Vikas&quot;&lt;/span&gt;;

    table.Add(obj1, obj1.Name);&lt;span class=&quot;rem&quot;&gt;// Here the Hascode of Obj1 is stored. &lt;/span&gt;

    obj1.Name = &lt;span class=&quot;str&quot;&gt;&quot;Changed &quot;&lt;/span&gt;; &lt;span class=&quot;rem&quot;&gt;// Here HasCode of obj1 will be changed due to our bad implementation&lt;/span&gt;

    Object name = table[obj1];&lt;span class=&quot;rem&quot;&gt;// will return Null .      &lt;/span&gt;
 
  }
}
&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; A : IEquatable&amp;lt;A&amp;gt;
 
{
  &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; String _name;
  &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; String Name
  {
    get
    {
      &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; _name;
    }
    set
    {
      _name = &lt;span class=&quot;kwrd&quot;&gt;value&lt;/span&gt;;
    }
  }
  &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; GetHashCode()
  {
    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; Name.GetHashCode();
  }
  &lt;span class=&quot;preproc&quot;&gt;#region&lt;/span&gt; IEquatable&amp;lt;A&amp;gt; Members

  &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; Equals(A other)
  {
    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (String.Compare(Name, other.Name, StringComparison.OrdinalIgnoreCase) == 0)
      &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;;
    &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt;
      &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;false&lt;/span&gt;;
  }

  &lt;span class=&quot;preproc&quot;&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;&lt;br /&gt;
&lt;/HR&gt;When defining Hash Code try to follow these guidelines: &lt;br /&gt;
&lt;br /&gt;
1)Use an algorithm that gives good random distribution for best  performance. &lt;br /&gt;
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.&lt;br /&gt;
3)Your algorithm should be executed very quickly. &lt;br /&gt;
4)Object with the same value should return same hash code.&lt;br /&gt;
5)You can use base.GetHashCode if you want.&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
System.ValueType’s implementation of GetHashCode function uses reflection so we should avoid using it because reflection is slow.&lt;br /&gt;
&lt;br /&gt;
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.</content><link rel='replies' type='application/atom+xml' href='http://vikas-ksharma.blogspot.com/feeds/2424936678865940728/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6783752066256544375/2424936678865940728?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/2424936678865940728'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/2424936678865940728'/><link rel='alternate' type='text/html' href='http://vikas-ksharma.blogspot.com/2010/07/equality-in-c-part-2gethashcode.html' title='Equality in C# – Part 2(GetHashCode)'/><author><name>vikas</name><uri>http://www.blogger.com/profile/15473935587636791410</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHa3cok5b8XpydIRDonXD-s-ezQ9e2ABuzunQYtYQL1VH9Lcr4y_Fn229ipUP1tSaVK-132qsl8kRzP4mNwtyHqM7cJLFG9mNIzq0_YmKEJ_G-tOOnTt5VcSXKo-5uei8/s220/vikas.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6783752066256544375.post-4929111364548901385</id><published>2010-07-01T13:02:00.003+08:00</published><updated>2010-07-05T11:42:19.985+08:00</updated><title type='text'>Equality in C# – Part 1</title><content type='html'>&lt;a href=&quot;http://anyurl.com/&quot; rel=&quot;tag&quot; style=&quot;display: none;&quot;&gt;CodeProject&lt;/a&gt;&lt;br /&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, &quot;Courier New&quot;, Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;&lt;br /&gt;
In normal programming practice we always have a need to compare two variables. In C# we have two major choices for accomplishing this task &lt;br /&gt;
&lt;br /&gt;
1) Using == operator &lt;br /&gt;
&lt;br /&gt;
2) Using .Equals method. &lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
Lets talk about behavior of == and .Equals in different cases &lt;br /&gt;
&lt;br /&gt;
A) &lt;b&gt;&lt;u&gt;Value type&lt;/u&gt;&lt;/b&gt; : (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). &lt;br /&gt;
&lt;br /&gt;
B)&lt;b&gt;&lt;u&gt;String Type&lt;/u&gt;&lt;/b&gt;: implementation of String overloads both == and .Equal methods to ensure equality of content not identity.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;&lt;pre class=&quot;csharpcode&quot; style=&quot;background-color: #dddddd;&quot;&gt;String s1 = &lt;span class=&quot;str&quot;&gt;&quot;test&quot;&lt;/span&gt;;
      String s2=&lt;span class=&quot;str&quot;&gt;&quot;test&quot;&lt;/span&gt;;

      &lt;span class=&quot;rem&quot;&gt;//true ,true , true&lt;/span&gt;
      Console.WriteLine (String.Format(&lt;span class=&quot;str&quot;&gt;&quot;{0} {1} {2}&quot;&lt;/span&gt;,s1==s2,s1.Equals(s2), &lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt;.ReferenceEquals (s1,s2))); 

      &lt;span class=&quot;rem&quot;&gt;// Now lets change their refrence.&lt;/span&gt;
      s1 = &lt;span class=&quot;str&quot;&gt;&quot;1test&quot;&lt;/span&gt;.Substring(1);

      &lt;span class=&quot;rem&quot;&gt;//output true ,true , false       &lt;/span&gt;
      Console.WriteLine(String.Format(&lt;span class=&quot;str&quot;&gt;&quot;{0} {1} {2}&quot;&lt;/span&gt;, s1 == s2, s1.Equals(s2), &lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt;.ReferenceEquals(s1, s2)));
 &lt;/pre&gt;&lt;hr /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
C) &lt;b&gt;&lt;u&gt;Other Reference Type&lt;/u&gt;&lt;/b&gt; : For other reference types we need to give our own implantation of .Equals and == operator. &lt;br /&gt;
&lt;br /&gt;
Rules to implement .Equal method &lt;br /&gt;
&lt;br /&gt;
1) If you are planning to implement .Equal method then try to implement &lt;br /&gt;
through IEquatable interface to ensure type safety implementation.&lt;br /&gt;
2) Overload == and != operator methods to ensure same functionality &lt;br /&gt;
as .Equal overloaded method&lt;br /&gt;
3) Avoid calling base.Equals method. &lt;br /&gt;
4) Implement GetHasCode() method with .Equal method. &lt;br /&gt;
&lt;br /&gt;
In   &lt;a href=&quot;http://vikas-ksharma.blogspot.com/2010/07/equality-in-c-part-2gethashcode.html&quot;&gt;next part&lt;/a&gt; I will explain why GetHashCode() method is necessary while implementing .Equals method.</content><link rel='replies' type='application/atom+xml' href='http://vikas-ksharma.blogspot.com/feeds/4929111364548901385/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6783752066256544375/4929111364548901385?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/4929111364548901385'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/4929111364548901385'/><link rel='alternate' type='text/html' href='http://vikas-ksharma.blogspot.com/2010/07/equality-in-c-part-1.html' title='Equality in C# – Part 1'/><author><name>vikas</name><uri>http://www.blogger.com/profile/15473935587636791410</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHa3cok5b8XpydIRDonXD-s-ezQ9e2ABuzunQYtYQL1VH9Lcr4y_Fn229ipUP1tSaVK-132qsl8kRzP4mNwtyHqM7cJLFG9mNIzq0_YmKEJ_G-tOOnTt5VcSXKo-5uei8/s220/vikas.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6783752066256544375.post-8503858206632918689</id><published>2010-03-11T15:26:00.000+08:00</published><updated>2010-03-11T15:26:47.874+08:00</updated><title type='text'>MVC Architectural Pattern ( Part 1)  : Basic Concepts</title><content type='html'>&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;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….&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;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 : &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;1) MVC &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;2) MVP&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;3) Multitier Architecture ( 3 tier )&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;4) Peer to peer. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;MVC concent was indtroduced by SmallTalk’s inventors &lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Trygve M. H. Reenskaug. MVC contain tree major components &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;1) Modal &lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;: Connected to user’s mind &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;2) View &lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Connected to user’s eyes &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;3) Controller: Connected to user’s hands. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;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 &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;1) Modal is our object represeting data. Modal are &lt;strong&gt;SMART&lt;/strong&gt;. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;2) View is visualization of state of model. View are &lt;strong&gt;DUMBS &lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;3) Controler facilate the change of state of model. It takes user input. Controller are &lt;strong&gt;THIN&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;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. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;u&gt;&lt;strong&gt;MVC Pasive Mode&lt;/strong&gt; &lt;/u&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;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. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;strong&gt;&lt;u&gt;The Varition&lt;/u&gt;&lt;/strong&gt; &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;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. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;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. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://vikas-ksharma.blogspot.com/feeds/8503858206632918689/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6783752066256544375/8503858206632918689?isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/8503858206632918689'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/8503858206632918689'/><link rel='alternate' type='text/html' href='http://vikas-ksharma.blogspot.com/2010/03/mvc-architectural-pattern-part-1-basic.html' title='MVC Architectural Pattern ( Part 1)  : Basic Concepts'/><author><name>vikas</name><uri>http://www.blogger.com/profile/15473935587636791410</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHa3cok5b8XpydIRDonXD-s-ezQ9e2ABuzunQYtYQL1VH9Lcr4y_Fn229ipUP1tSaVK-132qsl8kRzP4mNwtyHqM7cJLFG9mNIzq0_YmKEJ_G-tOOnTt5VcSXKo-5uei8/s220/vikas.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6783752066256544375.post-225293624718079056</id><published>2010-03-05T17:05:00.001+08:00</published><updated>2010-03-05T17:10:38.506+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Performence"/><category scheme="http://www.blogger.com/atom/ns#" term="TreeView"/><category scheme="http://www.blogger.com/atom/ns#" term="Winform"/><title type='text'>Updating Text in TreeView Control (Fastest Way)</title><content type='html'>You might be wondering why i am writing blog for simplest task like Updating a Node text in Treeview Control ( Winform ) ? &lt;br /&gt;
&lt;br /&gt;
Answer is performence !!!!&lt;br /&gt;
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.( &lt;i&gt;by the way What is the fastest way of finding the node in Tree View ? : Wait for my next blog... ;&lt;/i&gt;) ) . &lt;br /&gt;
&lt;br /&gt;
Ok lets come to the point again :  When we call function &lt;b&gt;Node.Text&lt;/b&gt;  TreeView tries to render itself and it takes time.  So what is the solution  ? &lt;br /&gt;
&lt;br /&gt;
The solution is to calll &lt;b&gt;TreeView.BeginUpdate &lt;/b&gt;before updating Text and call &lt;b&gt;TreeView.EndUpdate() &lt;/b&gt;after the text change operation. &lt;br /&gt;
&lt;br /&gt;
I have noticed performence increased 20 times with this method.  &lt;br /&gt;
&lt;br /&gt;
Thanks and have a good Weekend !</content><link rel='replies' type='application/atom+xml' href='http://vikas-ksharma.blogspot.com/feeds/225293624718079056/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6783752066256544375/225293624718079056?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/225293624718079056'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/225293624718079056'/><link rel='alternate' type='text/html' href='http://vikas-ksharma.blogspot.com/2010/03/updating-text-in-treeview-control.html' title='Updating Text in TreeView Control (Fastest Way)'/><author><name>vikas</name><uri>http://www.blogger.com/profile/15473935587636791410</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHa3cok5b8XpydIRDonXD-s-ezQ9e2ABuzunQYtYQL1VH9Lcr4y_Fn229ipUP1tSaVK-132qsl8kRzP4mNwtyHqM7cJLFG9mNIzq0_YmKEJ_G-tOOnTt5VcSXKo-5uei8/s220/vikas.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6783752066256544375.post-2609503679253414491</id><published>2010-02-26T19:12:00.006+08:00</published><updated>2010-03-03T14:13:27.798+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="IEnumerable"/><category scheme="http://www.blogger.com/atom/ns#" term="LINQ"/><title type='text'>IEnumerable and LINQ inside</title><content type='html'>&lt;a href=&quot;http://anyurl.com&quot; rel=&quot;tag&quot; style=&quot;display:none&quot;&gt;CodeProject&lt;/a&gt;&lt;br /&gt;
&lt;style  type =&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, &quot;Courier New&quot;, Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }




&lt;/style&gt;&lt;br /&gt;
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?  &lt;br /&gt;
&lt;br /&gt;
Yes our IEnumerable will be affected automatically.  Here is one example : &lt;br /&gt;
&lt;hr&gt;&lt;pre class=&quot;csharpcode&quot; style=&quot;background-color: #ddd&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System;
&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System.Linq;
&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System.Windows.Forms;

&lt;span class=&quot;kwrd&quot;&gt;namespace&lt;/span&gt; WindowsFormsApplication2
{
  &lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; Program
  {
    &lt;span class=&quot;rem&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;rem&quot;&gt;/// The main entry point for the application.&lt;/span&gt;
    &lt;span class=&quot;rem&quot;&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    [STAThread]
    &lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Main()
    {
      List&amp;lt;Person&amp;gt; ListOfPersons= &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; List&amp;lt;Person&amp;gt; {
      &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Person { Salary =1000, Name =&lt;span class=&quot;str&quot;&gt;&quot;Person1&quot;&lt;/span&gt;},
      &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Person { Salary =2000, Name =&lt;span class=&quot;str&quot;&gt;&quot;Person2&quot;&lt;/span&gt;}
      };
      Person Person3 = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Person { Salary =3000, Name =&lt;span class=&quot;str&quot;&gt;&quot;Person3&quot;&lt;/span&gt;};
      Person Person4 =  &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Person { Salary =4000, Name =&lt;span class=&quot;str&quot;&gt;&quot;Person4&quot;&lt;/span&gt;};

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

      IEnumerable&amp;lt;Person&amp;gt; BadSalaryList = from person &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; ListOfPersons
                                           &lt;span class=&quot;kwrd&quot;&gt;where&lt;/span&gt; person.Salary &amp;lt; 2500
                                           select person;

      &lt;span class=&quot;rem&quot;&gt;// Now above LINQ will be Executed &lt;/span&gt;
      Console.WriteLine(BadSalaryList.Count()); &lt;span class=&quot;rem&quot;&gt;// output 2 &lt;/span&gt;

      &lt;span class=&quot;rem&quot;&gt;//Lets change Person3 &lt;/span&gt;
      &lt;span class=&quot;rem&quot;&gt;// This will re execute the query &lt;/span&gt;
      Person3.Salary = 500; &lt;span class=&quot;rem&quot;&gt;// This person will be automatically  included in BadSalaryList        &lt;/span&gt;


      Console.WriteLine(BadSalaryList.Count()); &lt;span class=&quot;rem&quot;&gt;// output 3 Person3 Added in last &lt;/span&gt;

      &lt;span class=&quot;rem&quot;&gt;// Lets change salary of Person1    &lt;/span&gt;
      &lt;span class=&quot;rem&quot;&gt;// This will re execute the query &lt;/span&gt;
      ListOfPersons[0].Salary = 7000; 


      Console.WriteLine(BadSalaryList.Count()); &lt;span class=&quot;rem&quot;&gt;// output 3 Person3 Added in last &lt;/span&gt;
      
  &lt;span class=&quot;rem&quot;&gt;//    Console.WriteLine(BadSalaryList.Count());&lt;/span&gt;
      Console.ReadLine();

    }
  }

  &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; Person
  {
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; Salary
    {
      get;
      set;
    }
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; String Name
    {
      get;
      set;
    }
    


  }
}

&lt;/pre&gt;&lt;br /&gt;
&lt;hr&gt;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 &lt;br /&gt;
1) It will be changed based on LINQ conditions &lt;br /&gt;
2) We might need to see performance as it will re evaluate the query on each change. &lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
If we do not want collection to be modified then we can store output in List&lt;&gt; . Here is the example :&lt;br /&gt;
&lt;hr&gt;&lt;pre class=&quot;csharpcode&quot; style=&quot;background-color: #ddd&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System;
&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System.Linq;
&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System.Windows.Forms;

&lt;span class=&quot;kwrd&quot;&gt;namespace&lt;/span&gt; WindowsFormsApplication2
{
  &lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; Program
  {
    &lt;span class=&quot;rem&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;rem&quot;&gt;/// The main entry point for the application.&lt;/span&gt;
    &lt;span class=&quot;rem&quot;&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    [STAThread]
    &lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Main()
    {
      List&amp;lt;Person&amp;gt; ListOfPersons= &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; List&amp;lt;Person&amp;gt; {
      &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Person { Salary =1000, Name =&lt;span class=&quot;str&quot;&gt;&quot;Person1&quot;&lt;/span&gt;},
      &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Person { Salary =2000, Name =&lt;span class=&quot;str&quot;&gt;&quot;Person2&quot;&lt;/span&gt;}
      };
      Person Person3 = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Person { Salary =3000, Name =&lt;span class=&quot;str&quot;&gt;&quot;Person3&quot;&lt;/span&gt;};
      Person Person4 =  &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Person { Salary =4000, Name =&lt;span class=&quot;str&quot;&gt;&quot;Person4&quot;&lt;/span&gt;};

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

      List &amp;lt;Person&amp;gt; BadSalaryList = (from person &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; ListOfPersons
                                           &lt;span class=&quot;kwrd&quot;&gt;where&lt;/span&gt; person.Salary &amp;lt; 2500
                                           select person).ToList ();

      &lt;span class=&quot;rem&quot;&gt;// Now above LINQ will be Executed &lt;/span&gt;
      &lt;span class=&quot;rem&quot;&gt;// Only once &lt;/span&gt;
      Console.WriteLine(BadSalaryList.Count()); &lt;span class=&quot;rem&quot;&gt;// output 2 &lt;/span&gt;

      &lt;span class=&quot;rem&quot;&gt;//Lets change Person3       &lt;/span&gt;
      &lt;span class=&quot;rem&quot;&gt;//Query will not be executed &lt;/span&gt;
      Person3.Salary = 500; 
      Console.WriteLine(BadSalaryList.Count()); &lt;span class=&quot;rem&quot;&gt;// output 2&lt;/span&gt;

      &lt;span class=&quot;rem&quot;&gt;// Lets change salary of Person1    &lt;/span&gt;
      &lt;span class=&quot;rem&quot;&gt;//Query will not be executed &lt;/span&gt;
      ListOfPersons[0].Salary = 7000;

      Console.WriteLine(BadSalaryList.Count()); &lt;span class=&quot;rem&quot;&gt;// output 2 &lt;/span&gt;
      
  &lt;span class=&quot;rem&quot;&gt;//    Console.WriteLine(BadSalaryList.Count());&lt;/span&gt;
      Console.ReadLine();

    }
  }

  &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; Person
  {
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; Salary
    {
      get;
      set;
    }
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; String Name
    {
      get;
      set;
    }
    


  }
}
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://vikas-ksharma.blogspot.com/feeds/2609503679253414491/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6783752066256544375/2609503679253414491?isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/2609503679253414491'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/2609503679253414491'/><link rel='alternate' type='text/html' href='http://vikas-ksharma.blogspot.com/2010/02/ienumerable-and-linq-inside.html' title='IEnumerable and LINQ inside'/><author><name>vikas</name><uri>http://www.blogger.com/profile/15473935587636791410</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHa3cok5b8XpydIRDonXD-s-ezQ9e2ABuzunQYtYQL1VH9Lcr4y_Fn229ipUP1tSaVK-132qsl8kRzP4mNwtyHqM7cJLFG9mNIzq0_YmKEJ_G-tOOnTt5VcSXKo-5uei8/s220/vikas.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6783752066256544375.post-7215188533755569490</id><published>2010-02-22T16:10:00.009+08:00</published><updated>2010-03-03T14:13:08.600+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Delegate"/><title type='text'>Delegates Inside</title><content type='html'>&lt;a href=&quot;http://anyurl.com&quot; rel=&quot;tag&quot; style=&quot;display:none&quot;&gt;CodeProject&lt;/a&gt;&lt;br /&gt;
&lt;style  type =&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, &quot;Courier New&quot;, Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }




&lt;/style&gt;&lt;br /&gt;
&lt;br /&gt;
Delegates are one of most complex and simplest concept in .Net ;) .&lt;br /&gt;
&lt;br /&gt;
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 &quot;Target&quot;. &lt;br /&gt;
&lt;br /&gt;
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................ &lt;br /&gt;
&lt;br /&gt;
Lets take an example :&lt;br /&gt;
&lt;hr&gt;&lt;pre class=&quot;csharpcode&quot; style=&quot;background-color: #ddd&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System;

&lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; Program
{
[STAThread]
&lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Main()
{

EventHandelerClass handeler = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; EventHandelerClass(&lt;span class=&quot;str&quot;&gt;&quot;My Text&quot;&lt;/span&gt;);
delShow showMe = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; delShow(handeler.OnShow);
handeler.Text = &lt;span class=&quot;str&quot;&gt;&quot;Changed &quot;&lt;/span&gt;;
handeler = &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;;  &lt;span class=&quot;rem&quot;&gt;// set to null &lt;/span&gt;
GC.Collect(); &lt;span class=&quot;rem&quot;&gt;// Make sure it is released &lt;/span&gt;
Console.WriteLine(showMe(&lt;span class=&quot;str&quot;&gt;&quot; From Delegate&quot;&lt;/span&gt;));
Console.ReadLine();

}
}


&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;delegate&lt;/span&gt; String delShow(String msg);

&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; EventHandelerClass
{
&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; String Text
{
get;
set;
}
&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; EventHandelerClass(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; text)
{
Text = text;
}

&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; String OnShow(String msg)
{
&lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.Text + msg;
}


}
&lt;/pre&gt;&lt;br /&gt;
&lt;hr&gt;&lt;br /&gt;
Above program will display message &quot;My Text From Delegate&quot; even if handeler is set to null. &lt;br /&gt;
&lt;br /&gt;
This is because when you add a instance method to a delegate it store reference of that instance in its &quot;Target&quot; property. When we set handeler = null actually we are saying now &quot;handeler&quot; will no longer point to the address but delegate &quot;showMe&quot; is still pointing to same address ( ref How reference types are stored ? ). &lt;br /&gt;
&lt;br /&gt;
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 &quot;-=&quot; or &quot;MulticastDelegate.Remove&quot; method.&lt;br /&gt;
&lt;br /&gt;
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.</content><link rel='replies' type='application/atom+xml' href='http://vikas-ksharma.blogspot.com/feeds/7215188533755569490/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6783752066256544375/7215188533755569490?isPopup=true' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/7215188533755569490'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6783752066256544375/posts/default/7215188533755569490'/><link rel='alternate' type='text/html' href='http://vikas-ksharma.blogspot.com/2010/02/delegates-inside.html' title='Delegates Inside'/><author><name>vikas</name><uri>http://www.blogger.com/profile/15473935587636791410</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHa3cok5b8XpydIRDonXD-s-ezQ9e2ABuzunQYtYQL1VH9Lcr4y_Fn229ipUP1tSaVK-132qsl8kRzP4mNwtyHqM7cJLFG9mNIzq0_YmKEJ_G-tOOnTt5VcSXKo-5uei8/s220/vikas.jpg'/></author><thr:total>5</thr:total></entry></feed>