<?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: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-6035033648485035988</id><updated>2011-11-27T15:32:59.948-08:00</updated><category term='C#'/><category term='System.Collections.Generic'/><category term='General'/><category term='Visual Studio Add-In'/><category term='Lambda'/><category term='.NET 3.5'/><title type='text'>Programing in C# .NET - The new MSDN</title><subtitle type='html'>For all C# .NET developers out there...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programing-csharp.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6035033648485035988/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://programing-csharp.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Alon</name><uri>http://www.blogger.com/profile/02646714544713339442</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6035033648485035988.post-1036000251919896377</id><published>2008-06-06T12:18:00.000-07:00</published><updated>2008-06-14T01:13:08.120-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='General'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET 3.5'/><category scheme='http://www.blogger.com/atom/ns#' term='Lambda'/><title type='text'>Lambda Expressions (sample) - The .Net delegate evolution...</title><content type='html'>For some strange reason many developers afraid from Lambda, I don't know why exactly but I think it is a great new syntax.&lt;br /&gt;A &lt;span class="term"&gt;lambda expression&lt;/span&gt; is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.&lt;br /&gt;&lt;br /&gt;A litle bit of background:&lt;br /&gt;In .Net 1.1 we had delegates.&lt;br /&gt;In .Net 2.0 we had anonymous delegates&lt;br /&gt;In .Net 3.5 we have lambda!!! (this is one of the best things that happens in .Net 3.5)&lt;br /&gt;&lt;br /&gt;small sample:&lt;br /&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;List&lt;/span&gt;&lt;int&gt;&lt;span style="color: rgb(51, 204, 255);"&gt; &lt;/span&gt;list = new &lt;span style="color: rgb(51, 204, 255);"&gt;List&lt;/span&gt;&lt;int&gt;();&lt;br /&gt;         &lt;span style="color: rgb(51, 102, 255);"&gt;int &lt;/span&gt;x = 5;&lt;br /&gt;         list.Find(NormalDelegate); &lt;span style="color: rgb(0, 153, 0);"&gt;//Notmal delegate&lt;/span&gt;&lt;br /&gt;&lt;/int&gt;&lt;/int&gt;private bool NormalDelegate(int number)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color: rgb(0, 153, 0);"&gt;// can't use anything but members and parameters (number)&lt;/span&gt;&lt;br /&gt;         return number%2 == 0;&lt;br /&gt;     }&lt;br /&gt;&lt;int&gt;&lt;int&gt;&lt;br /&gt;         list.Find(delegate(int num) &lt;span style="color: rgb(0, 153, 0);"&gt;//anonymous delegate &lt;/span&gt;&lt;br /&gt;                       {&lt;br /&gt;                           &lt;span style="color: rgb(0, 153, 0);"&gt;// can use x - or any thing in the block above plus parameters  &lt;/span&gt;&lt;br /&gt;                           &lt;span style="color: rgb(51, 51, 255);"&gt;return &lt;/span&gt;num%2 == 0;&lt;br /&gt;                       });&lt;br /&gt;&lt;br /&gt;         list.Find(num =&gt; num%2 == 0); &lt;/int&gt;&lt;/int&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;// The lambda syntax we don't need the return word&lt;/span&gt;&lt;int&gt;&lt;int&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Lambda:&lt;/span&gt;&lt;br /&gt;Well lambda is just a shorter why to write &lt;/int&gt;&lt;/int&gt;anonymous delegate&lt;br /&gt;The same code above can be write in lambda easly and very shortly&lt;br /&gt;list.Find(n =&gt; n % 2 ==9); //Thats it! one short line&lt;br /&gt;if you want to create something longer it will start to look like anonymous delegate&lt;br /&gt;For example&lt;br /&gt;list.Find(n=&gt; {&lt;br /&gt;            n = n + x;&lt;br /&gt;           &lt;span style="color: rgb(51, 51, 255);"&gt;return &lt;/span&gt;n % 2 == 0;&lt;br /&gt;                     });&lt;br /&gt;now we need the &lt;span style="color: rgb(51, 51, 255);"&gt;return &lt;/span&gt;"word" (Not like before)&lt;br /&gt;&lt;br /&gt;I hope it explain you abit about lambda&lt;br /&gt;&lt;br /&gt;You can read more about Lambda Expressions in the &lt;a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx"&gt;msdn&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Enjoy (e=&gt; e == &lt;span style="color: rgb(0, 204, 204);"&gt;Int32.&lt;/span&gt;MaxValue); // :)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;int&gt;&lt;int&gt;&lt;/int&gt;&lt;/int&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6035033648485035988-1036000251919896377?l=programing-csharp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programing-csharp.blogspot.com/feeds/1036000251919896377/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6035033648485035988&amp;postID=1036000251919896377' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6035033648485035988/posts/default/1036000251919896377'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6035033648485035988/posts/default/1036000251919896377'/><link rel='alternate' type='text/html' href='http://programing-csharp.blogspot.com/2008/06/lambda-delegate-evolution.html' title='Lambda Expressions (sample) - The .Net delegate evolution...'/><author><name>Alon</name><uri>http://www.blogger.com/profile/02646714544713339442</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6035033648485035988.post-9022217989782922204</id><published>2008-05-10T08:53:00.000-07:00</published><updated>2008-06-14T08:32:59.776-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET 3.5'/><title type='text'>Extension Methods C# .NET 3.5</title><content type='html'>Extension Methods are new at .NET 3.5 It let you add methods to existent class.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre id="ctl00_rs1_mainContentContainer_ctl06" class="libCScode" space="preserve"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;public static class&lt;/span&gt; &lt;/span&gt;Extensions&lt;br /&gt;{&lt;br /&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;public static int&lt;/span&gt; &lt;/span&gt;ToInt32Ext(&lt;span style="color: rgb(51, 51, 255); font-weight: bold; font-style: italic;"&gt;this&lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;string&lt;/span&gt; s) &lt;span style="color: rgb(0, 153, 0);"&gt;// we add the this word&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color: rgb(51, 102, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;Int32.Parse(s);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;public static int&lt;/span&gt; ToInt32(&lt;span style="color: rgb(51, 102, 255);"&gt;string &lt;/span&gt;s)&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color: rgb(51, 102, 255);"&gt;return&lt;/span&gt; Int32.Parse(s);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;Main()&lt;br /&gt;{&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;s =&lt;span style="color: rgb(255, 204, 255);"&gt; &lt;span style="color: rgb(153, 0, 0);"&gt;"5"&lt;/span&gt;&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;int&lt;/span&gt; x = s.ToInt32Ext(); &lt;span style="color: rgb(0, 102, 0);"&gt;// using the extension method&lt;/span&gt;&lt;br /&gt;Console.WriteLine(x);&lt;br /&gt;x = Extensions.ToInt32(s);&lt;br /&gt;Console.WriteLine(x);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Please notice:&lt;br /&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;The this word must be the first argument in the method,in this example we only have one argument but if you have more remember that it must be the first thing.&lt;/li&gt;&lt;li&gt;The class must be a static class.&lt;/li&gt;&lt;/ul&gt;I think it is a nice feature but doesn't really add new power to the language.&lt;br /&gt;&lt;pre id="ctl00_rs1_mainContentContainer_ctl06" class="libCScode" space="preserve"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6035033648485035988-9022217989782922204?l=programing-csharp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programing-csharp.blogspot.com/feeds/9022217989782922204/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6035033648485035988&amp;postID=9022217989782922204' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6035033648485035988/posts/default/9022217989782922204'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6035033648485035988/posts/default/9022217989782922204'/><link rel='alternate' type='text/html' href='http://programing-csharp.blogspot.com/2008/05/extension-methods-c-net-30.html' title='Extension Methods C# .NET 3.5'/><author><name>Alon</name><uri>http://www.blogger.com/profile/02646714544713339442</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6035033648485035988.post-1174171402803143314</id><published>2008-05-09T14:32:00.000-07:00</published><updated>2008-06-13T09:41:30.091-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='General'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Equals and GetHashCode The Right Way!!!!!</title><content type='html'>Hi&lt;br /&gt;Recently I witness many mistakes when creating new class. many developers override Object.GetHashCode() in a wrong way!!! that can make error that will be very hard to find. Lets start with the basic rules when writing your own GetHashCode:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;If X.Eqauls(Y) == true then X.GetHashCode() == Y.GetHashCode(). if two objects are equals they must have the same HashCode.&lt;/li&gt;&lt;li&gt;If X.GetHashCode() != Y.GetHashCode() then X.Equals(Y) == false. If two objects has defraent hash code they cant be Equal to each other.&lt;/li&gt;&lt;li&gt;object GetHashCode() must NOT be changed during it life cycle. Once the object was created it hash code must NOT change!!!&lt;/li&gt;&lt;li&gt;If X.Eqauls(Y) == false then X.GetHashCode() Should (NOT A MUST) be defrant from Y.GetHashCode()&lt;/li&gt;&lt;/ul&gt;If you creates your own object and overrides Equals you will get an Warning:&lt;br /&gt;&lt;span style="color: rgb(255, 204, 0);"&gt;&lt;/span&gt;Here is an example of a wrong way to make HashCode and what will happen:&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;&lt;br /&gt;class&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;MyWrongObject&lt;br /&gt;&lt;span id="ctl00_ContentPlaceHolder1_output"&gt;&lt;pre class="csharpcode"&gt;    {&lt;br /&gt;    &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;private&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;int&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;m_One;&lt;br /&gt;    &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;public&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;int&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;One&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);"&gt;get &lt;/span&gt;{ &lt;span style="color: rgb(51, 102, 255);" class="kwrd"&gt;return&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;m_One; }&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);"&gt;set &lt;/span&gt;{ m_One = &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;public&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;MyWrongObject() { }&lt;br /&gt;    &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;public&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;MyWrongObject(&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;int&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;one)&lt;br /&gt;    {&lt;br /&gt;        m_One = one;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;public&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;override&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;bool&lt;/span&gt; Equals(&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;object&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;obj)&lt;br /&gt;    {&lt;br /&gt;        MyWrongObject other = obj &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;as&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;MyWrongObject;&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;if&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;(other != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;            &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;return&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;(One == other.One);&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: rgb(51, 51, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;return&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;false&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;public&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;override&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;string&lt;/span&gt; ToString()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;return&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;String.Format(&lt;span style="color: rgb(204, 102, 0);" class="str"&gt;"MyWrongObject[{0}]"&lt;/span&gt;, One);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;public&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;override&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;int&lt;/span&gt; GetHashCode()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;return&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;ToString().GetHashCode();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;p&lt;span style="color: rgb(51, 51, 255);"&gt;ublic&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;static&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;void&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;Main(&lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;    {&lt;br /&gt;        HashSet&amp;lt;MyWrongObject&amp;gt; set1 = &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;new&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;HashSet&amp;lt;MyWrongObject&amp;gt;();&lt;br /&gt;        MyWrongObject obj1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; MyWrongObject(1, 1);&lt;br /&gt;        set1.Add(obj1);&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;if&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;(set1.Contains(obj1))&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Found obj1"&lt;/span&gt;);&lt;br /&gt;        obj1.One = 2; &lt;span style="color: rgb(0, 102, 0);" class="rem"&gt;// This line changes the GetHashCode() of obj1&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;if&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;(set1.Contains(obj1))&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="rem"&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;// We wont see this line because Contains looks at the GetHashCode()&lt;/span&gt;&lt;br /&gt;                then at Equals()&lt;/span&gt;&lt;br /&gt;            Console.WriteLine(&lt;span style="color: rgb(204, 102, 0);" class="str"&gt;"You won't see this line"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;            Console.WriteLine(&lt;span style="color: rgb(204, 102, 0);" class="str"&gt;"Hmmm.. can't find obj1 but obj1 is here"&lt;/span&gt;);&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;foreach&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;(MyWrongObject obj &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;in&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;set1)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: rgb(51, 51, 255);" class="kwrd"&gt;if&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;(obj1.Equals(obj))&lt;br /&gt;                Console.WriteLine(&lt;span style="color: rgb(204, 102, 0);" class="str"&gt;"obj:{0} Can't find obj1 but obj1 is here"&lt;/span&gt;, obj);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Please notice that you can't make a setter to a member that changes the GetHashCode() of the object.&lt;br /&gt;If you do that you should think about throwing an exeption when using GetHashCode()&lt;br /&gt;&lt;br /&gt;Enjoy,&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6035033648485035988-1174171402803143314?l=programing-csharp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programing-csharp.blogspot.com/feeds/1174171402803143314/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6035033648485035988&amp;postID=1174171402803143314' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6035033648485035988/posts/default/1174171402803143314'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6035033648485035988/posts/default/1174171402803143314'/><link rel='alternate' type='text/html' href='http://programing-csharp.blogspot.com/2008/05/equals-and-gethashcode-right-way.html' title='Equals and GetHashCode The Right Way!!!!!'/><author><name>Alon</name><uri>http://www.blogger.com/profile/02646714544713339442</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6035033648485035988.post-7933929862426102666</id><published>2008-04-30T13:36:00.000-07:00</published><updated>2008-05-01T14:04:58.313-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Visual Studio Add-In'/><title type='text'>Visual Studio Add-ons (Add-in)</title><content type='html'>Hi,&lt;br /&gt;I would like to tell you about few Visual Studio add ons that are a must for any developer.&lt;br /&gt;Let's start with the free ones...&lt;br /&gt;1. &lt;span style="font-weight: bold; font-style: italic;"&gt;Test Driven .NET&lt;/span&gt; - Let you run any function with a single click No more Main functions for testing your function code just click and go... You can run test, open reflector with a click this is a free download. Trust me it will make you life very easy &lt;a href="http://www.testdriven.net/"&gt;http://www.testdriven.net/&lt;/a&gt;&lt;br /&gt;2. &lt;span style="font-weight: bold; font-style: italic;"&gt;GhostDoc&lt;/span&gt; - GhostDoc is a free add-in for Visual Studio that automatically generates    XML&lt;br /&gt; documentation comments for C#. Either by using existing documentation    inherited&lt;br /&gt; from base classes or implemented interfaces, or by deducing comments    from&lt;br /&gt; name and type of e.g. methods, properties or parameters. &lt;a href="http://www.roland-weigelt.de/ghostdoc/"&gt;http://www.roland-weigelt.de/ghostdoc/&lt;/a&gt;&lt;br /&gt;3. &lt;span style="font-weight: bold; font-style: italic;"&gt;Resharper&lt;/span&gt; - This is add-in is not free but it will save you so much time and energy that will return it cost in less then a week. Great error finder, code optimizer and much more I can write a lot on it but I think you should read more in the site&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt; &lt;/span&gt;(30 days Trail is available &lt;a href="http://www.jetbrains.com/resharper/"&gt;here&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/a&gt;)&lt;br /&gt;4.&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt; &lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;Smart paster - &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: underline;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;let you paste any thing you want as a comment, string, StringBuilder or Region. very nice and easy to use adds another option at the right click menu (download from &lt;a href="http://thedailywtf.com/forums/76015"&gt;here&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;Enjoy&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6035033648485035988-7933929862426102666?l=programing-csharp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programing-csharp.blogspot.com/feeds/7933929862426102666/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6035033648485035988&amp;postID=7933929862426102666' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6035033648485035988/posts/default/7933929862426102666'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6035033648485035988/posts/default/7933929862426102666'/><link rel='alternate' type='text/html' href='http://programing-csharp.blogspot.com/2008/04/visual-studio-add-ons-add-in.html' title='Visual Studio Add-ons (Add-in)'/><author><name>Alon</name><uri>http://www.blogger.com/profile/02646714544713339442</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6035033648485035988.post-6819717485208717442</id><published>2008-04-25T06:20:00.000-07:00</published><updated>2008-05-10T09:16:02.088-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='System.Collections.Generic'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET 3.5'/><title type='text'>At last HashSet in .NET 3.5</title><content type='html'>Hi&lt;br /&gt;A few days ago I found a new collection HashSet&lt;t&gt; (System.Collections.Generic namespace).&lt;br /&gt;I am personally waited for this since I first saw it in JAVA 1.4.2.&lt;br /&gt;HashSet is a combination betwin a Set and A Dictionary&lt;key,value&gt;. Each element can be found only once. Add, Remove, Contains In O(1) - expected (if no rehash is required) just like Dictionary&lt;key,value&gt;.&lt;br /&gt;It has some new cool methods like UnionWith, IntersectWith.&lt;br /&gt;Small (and simple) Exmaple:&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;  &lt;/key,value&gt;&lt;/key,value&gt;&lt;/t&gt;&lt;span style="font-size:85%;"&gt;   &lt;/span&gt;&lt;t&gt;&lt;key,value&gt;&lt;key,value&gt;&lt;span style="color: rgb(102, 204, 204);font-size:85%;" &gt;HashSet&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;int&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&gt; theSet1 = &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;new &lt;/span&gt;&lt;span style="color: rgb(102, 204, 204);font-size:85%;" &gt; &amp;amp;nbspHashSet&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;int&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&gt;();&lt;br /&gt;  theSet1.Add(1);     &lt;/span&gt;&lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;// Return true&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/key,value&gt;&lt;/key,value&gt;&lt;/t&gt;&lt;span style="font-size:85%;"&gt;   &lt;/span&gt;&lt;t&gt;&lt;key,value&gt;&lt;key,value&gt;&lt;span style="font-size:85%;"&gt;   theSet1.Add(2);     &lt;/span&gt;&lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;// Return true&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/key,value&gt;&lt;/key,value&gt;&lt;/t&gt;&lt;span style="font-size:85%;"&gt;   &lt;/span&gt;&lt;t&gt;&lt;key,value&gt;&lt;key,value&gt;&lt;span style="font-size:85%;"&gt;   theSet1.Add(2);     &lt;/span&gt;&lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;// Return false&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;    // theSet1 contains 1,2&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;    &lt;span style="color: rgb(102, 204, 204);font-size:85%;" &gt;HashSet&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;int&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&gt; theSet2 = &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;new &lt;/span&gt;&lt;span style="color: rgb(102, 204, 204);font-size:85%;" &gt;HashSet&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;int&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&gt;();&lt;br /&gt;  theSet2.Add(1);     &lt;/span&gt;&lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;// Return true&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;  theSet2.Add(3);     &lt;/span&gt;&lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;// Return true&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;  theSet2.Add(4);     &lt;/span&gt;&lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;// Return true&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;   &lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt; // theSet2 contains 1,3,4&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;br /&gt;theSet1.UnionWith(theSet2);&lt;br /&gt;&lt;/span&gt;   &lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt; // theSet1 contains 1,2,3,4&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;br /&gt;theSet1.IntersectWith(theSet2);&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;    // theSet1 contains 2&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt; &lt;/span&gt;Unlike ICollection&lt;t&gt;.Add : void In HashSet.Add : bool&lt;/t&gt;&lt;/li&gt;&lt;/ul&gt;If you are using HashSet default constactur then you will get a defult comperator (just like in the example above). One thing that you might like to do is to create your own comperator for example we cna create a odd even comperator&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;    &lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;class &lt;/span&gt;&lt;span style="font-size:85%;"&gt;OddEvenComparer : &lt;/span&gt;&lt;span style="color: rgb(0, 204, 204);font-size:85%;" &gt;IEqualityComparer&lt;/span&gt;&lt;int&gt;&lt;span style="font-size:85%;"&gt; {&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;       public &lt;/span&gt;&lt;span style="font-size:85%;"&gt;OddEvenComparer() {}&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;       public bool &lt;/span&gt;&lt;span style="font-size:85%;"&gt;Equals(&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;int &lt;/span&gt;&lt;span style="font-size:85%;"&gt;x, &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;int &lt;/span&gt;&lt;span style="font-size:85%;"&gt;y) {&lt;br /&gt;         &lt;/span&gt;&lt;/int&gt;&lt;/key,value&gt;&lt;/key,value&gt;&lt;/t&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;       return&lt;/span&gt;&lt;t&gt;&lt;key,value&gt;&lt;key,value&gt;&lt;int&gt;&lt;span style="font-size:85%;"&gt; &lt;/span&gt;&lt;/int&gt;&lt;/key,value&gt;&lt;/key,value&gt;&lt;/t&gt;&lt;t&gt;&lt;key,value&gt;&lt;key,value&gt;&lt;int&gt;&lt;span style="font-size:85%;"&gt;(x &amp;amp; 1) == (y &amp;amp; 1);&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;     public int &lt;/span&gt;&lt;span style="font-size:85%;"&gt;GetHashCode(&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;int &lt;/span&gt;&lt;span style="font-size:85%;"&gt;x) {&lt;br /&gt;      &lt;/span&gt;&lt;/int&gt;&lt;/key,value&gt;&lt;/key,value&gt;&lt;/t&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;     return&lt;/span&gt;&lt;t&gt;&lt;key,value&gt;&lt;key,value&gt;&lt;int&gt;&lt;span style="font-size:85%;"&gt; (x &amp;amp; 1);&lt;br /&gt;     }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;    &lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;// Now we will use the comperator&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;    &lt;span style="color: rgb(102, 204, 204);font-size:85%;" &gt;HashSet&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;int&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&gt; &lt;/span&gt;&lt;/int&gt;&lt;/key,value&gt;&lt;/key,value&gt;&lt;span style="font-size:85%;"&gt;oddEvenSet&lt;/span&gt;&lt;key,value&gt;&lt;key,value&gt;&lt;int&gt;&lt;span style="font-size:85%;"&gt;= &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;new &lt;/span&gt;&lt;span style="color: rgb(102, 204, 204);font-size:85%;" &gt;HashSet&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;int&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&gt;(&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;new &lt;/span&gt;&lt;span style="color: rgb(102, 204, 204);font-size:85%;" &gt;OddEvenComparer&lt;/span&gt;&lt;span style="font-size:85%;"&gt;());&lt;br /&gt;  oddEvenSet.Add(1); &lt;/span&gt;&lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;// Return true&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;  oddEvenSet.Add(3); &lt;/span&gt;&lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;// Return false (we allready have an odd number)&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;  oddEvenSet.Add(4); &lt;/span&gt;&lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;// Return true&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;    &lt;span style="color: rgb(51, 204, 0);font-size:85%;" &gt;// oddEvenSet will now hold 1,4 (3 is equal to 1 accurding to our OddEvenComparer)&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;For more information about HashSet&lt;t&gt; you can read in the MSDN &lt;a href="http://msdn2.microsoft.com/en-us/library/bb359438.aspx"&gt;http://msdn2.microsoft.com/en-us/library/bb359438.aspx&lt;/a&gt;&lt;br /&gt;For Set definition from Wikipedia: &lt;/t&gt;&lt;/int&gt;&lt;/key,value&gt;&lt;/key,value&gt;&lt;a href="http://en.wikipedia.org/wiki/Set"&gt; http://en.wikipedia.org/wiki/Set&lt;/a&gt;&lt;br /&gt;&lt;key,value&gt;&lt;key,value&gt;&lt;int&gt;&lt;t&gt;&lt;br /&gt;Enjoy&lt;/t&gt;&lt;/int&gt;&lt;/key,value&gt;&lt;/key,value&gt;&lt;/t&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6035033648485035988-6819717485208717442?l=programing-csharp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programing-csharp.blogspot.com/feeds/6819717485208717442/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6035033648485035988&amp;postID=6819717485208717442' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6035033648485035988/posts/default/6819717485208717442'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6035033648485035988/posts/default/6819717485208717442'/><link rel='alternate' type='text/html' href='http://programing-csharp.blogspot.com/2008/04/at-last-hashset-in-net-35.html' title='At last HashSet&lt;T&gt; in .NET 3.5'/><author><name>Alon</name><uri>http://www.blogger.com/profile/02646714544713339442</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
