<?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-2833886219840350519</id><updated>2011-11-28T05:27:42.600+05:00</updated><category term='Optional Prameters'/><category term='CSharp 4.0'/><category term='LINQ'/><category term='ThreadPool'/><category term='Parallel.For'/><category term='TemplateBinding'/><category term='Covariance'/><category term='Thread'/><category term='.NET Framework 4.0'/><category term='dynamic'/><category term='IEnumerable'/><category term='VideoBrush'/><category term='Visual Studio 2010 Beta 1'/><category term='Consume First'/><category term='Break/Stop'/><category term='.NET Reflection'/><category term='Complex'/><category term='Generics'/><category term='Brush'/><category term='Delegates'/><category term='DrawingBrush'/><category term='C# 3.0'/><category term='Interfaces'/><category term='RadialGradientBrush'/><category term='ControlTemplate'/><category term='Contra-variance'/><category term='Anders Hejlsberg'/><category term='.NET 4.0'/><category term='SolidColorBrush'/><category term='Task Parallel Library'/><category term='TaskFactory'/><category term='Namded Prameters'/><category term='JavaScript'/><category term='CSharp 3.0'/><category term='Task'/><category term='Parallel LINQ'/><category term='Silverlight'/><category term='.NET'/><category term='C# 4.0'/><title type='text'>Windows Presentation Foundation  and Silverlight</title><subtitle type='html'>I Speak "C Sharp".</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>13</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-4469236123122238090</id><published>2009-06-04T10:47:00.010+06:00</published><updated>2009-06-04T12:20:59.328+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='TaskFactory'/><category scheme='http://www.blogger.com/atom/ns#' term='Task Parallel Library'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Visual Studio 2010 Beta 1'/><category scheme='http://www.blogger.com/atom/ns#' term='Task'/><title type='text'>Running Parallel Tasks</title><content type='html'>.NET 4.0 introduces a more "high level" construct than thread to write parallel program, System.Threading.Tasks.&lt;span style="color: rgb(51, 153, 153);"&gt;Task&lt;/span&gt;. Task is a unit of work which could be assigned on to a CPU core. This is a versatile and comprehensive API. You can wait/join a &lt;span style="color: rgb(51, 153, 153);"&gt;Task&lt;/span&gt;, cancel a &lt;span style="color: rgb(51, 153, 153);"&gt;Task&lt;/span&gt;, continue with another task when a task completes and more. When you need this functionality, you should try this class. &lt;span style="color: rgb(51, 153, 153);"&gt;Task &lt;/span&gt;class has a &lt;span style="color: rgb(51, 51, 255);"&gt;static &lt;/span&gt;property, Factory, of type &lt;span style="color: rgb(51, 153, 153);"&gt;TaskFactory&lt;/span&gt;. This class provides methods to create tasks from different options. The simplest way to create a task is, call the StartNew(Action act) method on &lt;span style="color: rgb(51, 153, 153);"&gt;Task&lt;/span&gt;.Factory, passing an Action delegate.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_VSqfPPWKu4Y/SidfEC1V88I/AAAAAAAAAEo/JfsxUL2NNIU/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 72px;" src="http://3.bp.blogspot.com/_VSqfPPWKu4Y/SidfEC1V88I/AAAAAAAAAEo/JfsxUL2NNIU/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5343344005983105986" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;By calling ContinueWith(Action&lt;task&gt;) on a task, we can specify another task which should be completed after a particular task has completed.&lt;br /&gt;You can wait on a task to complete, by calling wait method on task:&lt;br /&gt;&lt;/task&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_VSqfPPWKu4Y/SidgBJhnkzI/AAAAAAAAAEw/xabzHea1Aeo/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 123px;" src="http://1.bp.blogspot.com/_VSqfPPWKu4Y/SidgBJhnkzI/AAAAAAAAAEw/xabzHea1Aeo/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5343345055751443250" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;You can cancel a task asynchronously, by calling Cancel() on the task:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_VSqfPPWKu4Y/SidgXdLOvNI/AAAAAAAAAE4/_LnvAJr8IhI/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 101px;" src="http://1.bp.blogspot.com/_VSqfPPWKu4Y/SidgXdLOvNI/AAAAAAAAAE4/_LnvAJr8IhI/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5343345438983371986" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;You can cancel a task Synchronously, by calling CancelAndWait():&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_VSqfPPWKu4Y/SidgoVmWaeI/AAAAAAAAAFA/Yb8KzgxfhC0/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 106px;" src="http://1.bp.blogspot.com/_VSqfPPWKu4Y/SidgoVmWaeI/AAAAAAAAAFA/Yb8KzgxfhC0/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5343345729007413730" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;You can run a task synchronously, by calling RunSycnronously():&lt;br /&gt;&lt;img src="file:///C:/Users/Waqas/AppData/Local/Temp/moz-screenshot.jpg" alt="" /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_VSqfPPWKu4Y/Sidg97InukI/AAAAAAAAAFI/DyYd6CE-Lfs/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 104px;" src="http://4.bp.blogspot.com/_VSqfPPWKu4Y/Sidg97InukI/AAAAAAAAAFI/DyYd6CE-Lfs/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5343346099860519490" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-4469236123122238090?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/4469236123122238090/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/06/running-parallel-tasks.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/4469236123122238090'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/4469236123122238090'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/06/running-parallel-tasks.html' title='Running Parallel Tasks'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_VSqfPPWKu4Y/SidfEC1V88I/AAAAAAAAAEo/JfsxUL2NNIU/s72-c/01.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-7593529074559613599</id><published>2009-06-02T19:27:00.003+06:00</published><updated>2009-06-02T19:45:44.259+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Parallel LINQ'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='LINQ'/><title type='text'>From LINQ To Parallel LINQ</title><content type='html'>With .NET 4.0, Microsoft is introducing a new Language INtegrated Query (LINQ) i.e. Parallel Language INtegrated Query (PLinq). With PLinq, we can query "in memory" collections and objects in parallel. Mind it, we can query in memory objects in parallel not the Linq to SQL. Because, parallellizing Linq to SQL means to shift the overhead to SQL Server. For that purpose we have to wait for a new version of SQL Server.&lt;br /&gt;For PLinq System.Linq.&lt;span style="color: rgb(51, 153, 153);"&gt;ParallelEnumerable &lt;/span&gt;class. This class defines all the query operators which were provided with LINQ. The method to write PLinq query is just that simple. Consider a LINQ query:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiUry2EvL9I/AAAAAAAAAEQ/XqNQV_fhSNU/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 132px;" src="http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiUry2EvL9I/AAAAAAAAAEQ/XqNQV_fhSNU/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5342724685453799378" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;This is our old friend from C# 3.0. Now, to make it a PLinq query, we will only call AsParallel() extension method on the collection after &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; keywork:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_VSqfPPWKu4Y/SiUry3FJjyI/AAAAAAAAAEY/GsOrUWJBtBc/s1600-h/02.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 130px;" src="http://4.bp.blogspot.com/_VSqfPPWKu4Y/SiUry3FJjyI/AAAAAAAAAEY/GsOrUWJBtBc/s400/02.png" alt="" id="BLOGGER_PHOTO_ID_5342724685723963170" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;This method, AsParallel(), returns an object of type &lt;span style="color: rgb(51, 153, 153);"&gt;ParallelEnumerable&lt;/span&gt;. And then the &lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt;, &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt;, &lt;span style="color: rgb(51, 51, 255);"&gt;groupby&lt;/span&gt;, &lt;span style="color: rgb(51, 51, 255);"&gt;thenby &lt;/span&gt;keywords are mapped to the extenshion methods from &lt;span style="color: rgb(51, 153, 153);"&gt;ParallelEnumerable&lt;/span&gt;. For this code, I used the old prime determination algorithm defined as:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_VSqfPPWKu4Y/SiUrzORYaqI/AAAAAAAAAEg/FhVp5UwZHbs/s1600-h/Code01.png"&gt;&lt;img style="cursor: pointer; width: 393px; height: 191px;" src="http://4.bp.blogspot.com/_VSqfPPWKu4Y/SiUrzORYaqI/AAAAAAAAAEg/FhVp5UwZHbs/s400/Code01.png" alt="" id="BLOGGER_PHOTO_ID_5342724691949284002" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-7593529074559613599?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/7593529074559613599/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/06/from-linq-to-parallel-linq.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/7593529074559613599'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/7593529074559613599'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/06/from-linq-to-parallel-linq.html' title='From LINQ To Parallel LINQ'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiUry2EvL9I/AAAAAAAAAEQ/XqNQV_fhSNU/s72-c/01.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-472765275473506483</id><published>2009-06-01T18:32:00.003+06:00</published><updated>2009-06-01T19:06:00.202+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Task Parallel Library'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Visual Studio 2010 Beta 1'/><title type='text'>Parallel.ForEach and Parallel.Invoke</title><content type='html'>Parallel.ForEach&lt;tsource&gt; method is the parallel equivalent to "serial" foreach loop. It Iterates on a collection of generic type parameter type TSource. You can pass three different collection types: System.Collections.Generic.IEnumerable&lt;tsource&gt;, System.Collections.Concurrent.Partitioner&lt;tsource&gt; and System.Collections.Concurrent.OrderablePartitioner&lt;tsource&gt;. Partioner classes are abstract and are here to write custom partioning code. This topic is beyond the scope of this post, and leave it for the future.&lt;br /&gt;Here is a quick example of how to use this overload of Parallel.ForEach&lt;tsource&gt;:&lt;br /&gt;&lt;/tsource&gt;&lt;/tsource&gt;&lt;/tsource&gt;&lt;/tsource&gt;&lt;/tsource&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiPQd0gRH-I/AAAAAAAAAEA/HVQlMSex9co/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 146px;" src="http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiPQd0gRH-I/AAAAAAAAAEA/HVQlMSex9co/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5342342793720307682" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;ints is IEnumerable&lt;int&gt; and a is an object from this IEnumerable instance. You can use other Stop and Break semantics by replacing the second parameter with other delegate:&lt;br /&gt;Action&lt;int,&gt;. and use the ParallelLoopState.Stop or ParallelLoopState.Break to exit from "loop" at the earliest convenience.&lt;br /&gt;Lastly, lets see what's the Parallel.Invoke method has to offer. It takes a variable number of Action delegates, and executes them in parallel. Here is a quick example:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_VSqfPPWKu4Y/SiPQd62GkiI/AAAAAAAAAEI/fP3DeraR-kc/s1600-h/02.png"&gt;&lt;img style="cursor: pointer; width: 389px; height: 191px;" src="http://3.bp.blogspot.com/_VSqfPPWKu4Y/SiPQd62GkiI/AAAAAAAAAEI/fP3DeraR-kc/s400/02.png" alt="" id="BLOGGER_PHOTO_ID_5342342795422503458" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Task1,Task2,Task3,Task4 are dumb methods whose signature matches the Action delegate's, Just to show an example.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-472765275473506483?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/472765275473506483/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/06/parallelforeach-and-parallelinvoke.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/472765275473506483'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/472765275473506483'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/06/parallelforeach-and-parallelinvoke.html' title='Parallel.ForEach&lt;TResult&gt; and Parallel.Invoke'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiPQd0gRH-I/AAAAAAAAAEA/HVQlMSex9co/s72-c/01.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-8628268602656275532</id><published>2009-05-31T13:31:00.009+06:00</published><updated>2009-06-01T18:32:14.058+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Task Parallel Library'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Visual Studio 2010 Beta 1'/><title type='text'></title><content type='html'>I would like to close this month with the comparison of Parallel.For and serial for loop. For comparison, I took an INEFFICIENT algorithm to find if a given number is prime. Here is the algorithm:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiKkialkSgI/AAAAAAAAADY/S-DLhdx6bCY/s1600-h/Code01.png"&gt;&lt;img style="cursor: pointer; width: 393px; height: 191px;" src="http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiKkialkSgI/AAAAAAAAADY/S-DLhdx6bCY/s400/Code01.png" alt="" id="BLOGGER_PHOTO_ID_5342013019174226434" border="0" /&gt;&lt;/a&gt;&lt;p&gt;&lt;br /&gt;   Here is the serial for loop to find the primes from 2 to 100000:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_VSqfPPWKu4Y/SiKkz7GcaoI/AAAAAAAAADg/69aG_4dkRP4/s1600-h/Codeser.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 104px;" src="http://4.bp.blogspot.com/_VSqfPPWKu4Y/SiKkz7GcaoI/AAAAAAAAADg/69aG_4dkRP4/s400/Codeser.png" alt="" id="BLOGGER_PHOTO_ID_5342013319959833218" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;    And here is its output:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_VSqfPPWKu4Y/SiKlG2BQkaI/AAAAAAAAADo/8yE1hGZf7jg/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 395px;" src="http://4.bp.blogspot.com/_VSqfPPWKu4Y/SiKlG2BQkaI/AAAAAAAAADo/8yE1hGZf7jg/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5342013645013422498" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;   Note down the Thread ID, and it took 24511 milliseconds. And here is its equivalent Parallel code:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_VSqfPPWKu4Y/SiKlT_wD1LI/AAAAAAAAADw/ZMGkOJ4HaM4/s1600-h/Codepara.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 121px;" src="http://3.bp.blogspot.com/_VSqfPPWKu4Y/SiKlT_wD1LI/AAAAAAAAADw/ZMGkOJ4HaM4/s400/Codepara.png" alt="" id="BLOGGER_PHOTO_ID_5342013870963938482" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;   Here comes the output:&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiKldEApPXI/AAAAAAAAAD4/0sjaS8K2h-w/s1600-h/02.png"&gt;&lt;img style="cursor: pointer; width: 326px; height: 341px;" src="http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiKldEApPXI/AAAAAAAAAD4/0sjaS8K2h-w/s400/02.png" alt="" id="BLOGGER_PHOTO_ID_5342014026726063474" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;    Did you notice that 6 different threads run the code in parallel and it took 18220 milliseconds? The Parallel class uses Task class behind the scenes, to parallelize the loop iterations. And depending upon the number of cores of your CPU, it determines the optimal number of threads and assigns these tasks to the threads.&lt;br /&gt;&lt;p&gt;    This program determines prime number in a big range of 100000 numbers. But if you replace the both programs for less numbers, say 5000, or 10000, then the serial for loop will be more efficient. Because, Task objects creation incurs overhead and exceeds the solution time. So, if your problem is small try to avoid parallelize your code.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-8628268602656275532?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/8628268602656275532/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/05/i-would-like-to-close-this-month-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/8628268602656275532'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/8628268602656275532'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/05/i-would-like-to-close-this-month-with.html' title=''/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiKkialkSgI/AAAAAAAAADY/S-DLhdx6bCY/s72-c/Code01.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-2766593382870251372</id><published>2009-05-30T17:17:00.004+06:00</published><updated>2009-05-30T17:36:26.304+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Task Parallel Library'/><category scheme='http://www.blogger.com/atom/ns#' term='Parallel.For'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Break/Stop'/><title type='text'>Breaking and Stopping Parallel Loop in Task Parallel Library</title><content type='html'>What if you want to exit from a Parallel.For method, like you could with for loop? You can not write code like this:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_VSqfPPWKu4Y/SiEYcW90hjI/AAAAAAAAADE/B_Juks-lohg/s1600-h/02.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 152px;" src="http://1.bp.blogspot.com/_VSqfPPWKu4Y/SiEYcW90hjI/AAAAAAAAADE/B_Juks-lohg/s400/02.png" alt="" id="BLOGGER_PHOTO_ID_5341577508518397490" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;The problem here is that, For is a method of class Parallel. It is not  a loop. And you can not exit from a method except with return statement.&lt;br /&gt;The Solution:&lt;br /&gt;TPL Team came with a great idea. You can pass in a delegate, which takes two parameters. On is the loop counter, and the other is of type class ParallelLoopState. ParallelLoopState has two methods to deal with exiting from loop:   &lt;em&gt;&lt;strong&gt;&lt;br /&gt;Parallel.Break() &lt;/strong&gt;is shared with all other concurrent threads in the system which are participating in the loop's execution. After calling Break(), no additional iterations past the iteration of the caller will be executed on the current thread, and other parallel workers will be stopped at their earliest convenience.&lt;/em&gt;    &lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;br /&gt;Parallel.Stop() &lt;/strong&gt;is shared with all other concurrent threads in the system which are participating in the loop's execution. After calling Stop(), no additional iterations will be executed on the current thread, and other parallel workers will be stopped at their earliest convenience.&lt;br /&gt;&lt;br /&gt;So, to stop the loop you should write the code as follows:&lt;br /&gt;&lt;/em&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiEZoOE-ZqI/AAAAAAAAADM/AMO8iUl2JVg/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 156px;" src="http://2.bp.blogspot.com/_VSqfPPWKu4Y/SiEZoOE-ZqI/AAAAAAAAADM/AMO8iUl2JVg/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5341578811802543778" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;For a comprehensive overview of Parallel visit &lt;a href="http://www.lovethedot.net/2009/02/parallelfor-deeper-dive-parallel.html" target="_blank"&gt;This Blog Post&lt;/a&gt;&lt;br /&gt;&lt;blockquote&gt; &lt;/blockquote&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-2766593382870251372?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/2766593382870251372/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/05/breaking-and-stopping-parallel-loop-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/2766593382870251372'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/2766593382870251372'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/05/breaking-and-stopping-parallel-loop-in.html' title='Breaking and Stopping Parallel Loop in Task Parallel Library'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_VSqfPPWKu4Y/SiEYcW90hjI/AAAAAAAAADE/B_Juks-lohg/s72-c/02.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-3916610548458716399</id><published>2009-05-29T17:11:00.005+06:00</published><updated>2009-05-29T18:23:57.030+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Task Parallel Library'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET Framework 4.0'/><title type='text'>Parallel Programming with .NET Framework 4.0</title><content type='html'>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;In the mscorlib.dll version 4.0.0.0 there are three main namespaces: System.Threading, System.Threading.Tasks and System.Collections.Concurrent. System.Threading contains types like ThreadLocal&lt;t&gt;, ParallelOptions, ParallelLoopState, ParallelLoopResult, Parallel, ManualResetEventSlim to name a few.&lt;/t&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Threading.Tasks contains Task, Task&lt;tresult&gt;, TaskFactory etc to use the Task level parallelism. While the System.Collections.Concurrent contains the generic collections to work with data in thread save manner.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;To get started with TPL, there is simplest class Parallel in .NET Framework 4.0. Lets see what does it have to offer. It has four main methods Parallel.For, Parallel.For&lt;&gt;, Parallel.ForEach&lt;&gt; and Parallel.Invoke. First three methods are, as their names imply, provied to parallelize the loops. These methods have a lot of overloads to cover vast scenarios. While, Parallel.Invoke method has two overloads and just takes the array of Action delegate and invokes them in parallel.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;And here is an example of simplest for method:&lt;br /&gt;&lt;/tresult&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_VSqfPPWKu4Y/Sh_HpdZyIzI/AAAAAAAAAC0/_sbW1osyYGM/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 356px; height: 174px;" src="http://1.bp.blogspot.com/_VSqfPPWKu4Y/Sh_HpdZyIzI/AAAAAAAAAC0/_sbW1osyYGM/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5341207198166033202" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Parallel.For takes three arguments: two integers and the third parameter Action&lt;int&gt; delegate. This third delegate parameter is executed parallel on a multi-core machine.&lt;br /&gt;&lt;/int&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-3916610548458716399?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/3916610548458716399/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/05/parallel-programming-with-net-framework.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/3916610548458716399'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/3916610548458716399'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/05/parallel-programming-with-net-framework.html' title='Parallel Programming with .NET Framework 4.0'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_VSqfPPWKu4Y/Sh_HpdZyIzI/AAAAAAAAAC0/_sbW1osyYGM/s72-c/01.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-5472209642353451987</id><published>2009-05-28T19:44:00.005+06:00</published><updated>2009-05-28T21:37:43.401+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='ThreadPool'/><category scheme='http://www.blogger.com/atom/ns#' term='Task Parallel Library'/><category scheme='http://www.blogger.com/atom/ns#' term='Thread'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 4.0'/><title type='text'>Task Parallel Library (TPL) in .NET 4.0</title><content type='html'>&lt;div style="text-align: justify;"&gt;       Untill now, I have written about major enhancements to C# 4.0. Today, I want to talk about what is coming with the .NET Framework 4.0. There are a lot of new things coming with .NET 4.0, WPF 4.0, WCF 4.0, WF 4.0, Code Contracts to name a few. But for the matter of this post, I would like to throw light on the Task Parallel Library (TPL from now on, to save me typing).&lt;br /&gt;TPL is the enhancement to the .NET Framework to make concurrent programming on multi core systems easier. We had an option previously to write parallel programs, Threads, but it was "expensive" and difficult. With "expensive", I mean, you have to create a thread object and it incurs system overhead. And by "difficult", I mean, it is very difficult to synchronize access to shared resources and finding dead locks etc.&lt;br /&gt;&lt;/div&gt;        The .NET Framework 4.0 provides another "high-level" layer on top of the stack to make our lives easier. Now, we don't need to create "Thread" ourselves. We only create a Task (class), and pass in the delegate or method to execute. The underlying framework determines itself either to create a thread or not for the particular task or to assign it to already executing task. Before I go into the details about the TPL, I would like you to read this post by &lt;a href="http://www.danielmoth.com/Blog/2008/11/new-and-improved-clr-4-thread-pool.html" target="_blank"&gt;Daniel moth.&lt;/a&gt;&lt;br /&gt;In which he describes the "major over hauling" of ThreadPool in .NET 4.0.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-5472209642353451987?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/5472209642353451987/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/05/task-parallel-library-tpl-in-net-40.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/5472209642353451987'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/5472209642353451987'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/05/task-parallel-library-tpl-in-net-40.html' title='Task Parallel Library (TPL) in .NET 4.0'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-6020255705359563667</id><published>2009-05-27T20:37:00.012+06:00</published><updated>2009-05-27T20:59:00.644+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Visual Studio 2010 Beta 1'/><category scheme='http://www.blogger.com/atom/ns#' term='Consume First'/><title type='text'>C# 4.0 Consume First</title><content type='html'>C# 4.0 is coming with a great new feature, Consume First. You can consume any type first and define it after consuming. Lets jump straight into the coding, and see how does it work. Start by creating a Console Application, and type a class name, that is not declared, in the main method. I used the ConsumeFirstClass for this example. The intellisence doesn't show the class:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1RExLoGSI/AAAAAAAAABk/r8w44oOCrnI/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 302px;" src="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1RExLoGSI/AAAAAAAAABk/r8w44oOCrnI/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5340513875494705442" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;when you type the assignment operator, =, and new the intellisense starts showing this class:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1RWrOpHgI/AAAAAAAAABs/JZbl5RvoT88/s1600-h/02.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 273px;" src="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1RWrOpHgI/AAAAAAAAABs/JZbl5RvoT88/s400/02.png" alt="" id="BLOGGER_PHOTO_ID_5340514183134387714" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Remember the class name is underlined because we don't have defined it. Now write an arbitrary method, I wrote ConsumeFirstMethod:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_VSqfPPWKu4Y/Sh1RvjLrdgI/AAAAAAAAAB0/cE3mBzw6hIA/s1600-h/03.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 128px;" src="http://1.bp.blogspot.com/_VSqfPPWKu4Y/Sh1RvjLrdgI/AAAAAAAAAB0/cE3mBzw6hIA/s400/03.png" alt="" id="BLOGGER_PHOTO_ID_5340514610471204354" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Our class object is showing in the intellisense. And pass in the string parameter, you can pass any thing, the method takes as parameter.&lt;br /&gt;Now place the cursor on the ConsumeFirstClass variable declaration and press Ctrl + . to show the available options:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_VSqfPPWKu4Y/Sh1SxmYcX0I/AAAAAAAAAB8/mWxgureIrh8/s1600-h/04.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 159px;" src="http://2.bp.blogspot.com/_VSqfPPWKu4Y/Sh1SxmYcX0I/AAAAAAAAAB8/mWxgureIrh8/s400/04.png" alt="" id="BLOGGER_PHOTO_ID_5340515745201413954" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Select "Generate class for 'ConsumeFirstClass' ". Now visual studio 2010 places a new file named ConsumeFirstClass.cs.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1TDjtBBUI/AAAAAAAAACE/CjuCBNIszCU/s1600-h/04.png"&gt;&lt;img style="cursor: pointer; width: 288px; height: 217px;" src="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1TDjtBBUI/AAAAAAAAACE/CjuCBNIszCU/s400/04.png" alt="" id="BLOGGER_PHOTO_ID_5340516053720040770" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Now go the the method call to ConsumeFirstMethod, press Ctrl + . (dot) and select Generate method stub ...&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1TUvFBjsI/AAAAAAAAACM/Gf8r0RUTZj8/s1600-h/05.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 53px;" src="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1TUvFBjsI/AAAAAAAAACM/Gf8r0RUTZj8/s400/05.png" alt="" id="BLOGGER_PHOTO_ID_5340516348831305410" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;The class definition looks something like this:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1TfVfmBvI/AAAAAAAAACU/bqkSJpxwoj0/s1600-h/06.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 112px;" src="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1TfVfmBvI/AAAAAAAAACU/bqkSJpxwoj0/s400/06.png" alt="" id="BLOGGER_PHOTO_ID_5340516530941986546" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;The visual studio 2010 class generator is smart enough that it determined our method signature, it takes a string and returns an int.&lt;br /&gt;Now remove the throw statement and place a return statement:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_VSqfPPWKu4Y/Sh1T-TOAurI/AAAAAAAAACc/iarbA_b-qD0/s1600-h/07.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 118px;" src="http://1.bp.blogspot.com/_VSqfPPWKu4Y/Sh1T-TOAurI/AAAAAAAAACc/iarbA_b-qD0/s400/07.png" alt="" id="BLOGGER_PHOTO_ID_5340517062907312818" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;And here is the output...&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1Uf34Q_GI/AAAAAAAAACk/ETO4M4qlJ1Y/s1600-h/bomb_blast.jpg"&gt;&lt;img style="cursor: pointer; width: 298px; height: 400px;" src="http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1Uf34Q_GI/AAAAAAAAACk/ETO4M4qlJ1Y/s400/bomb_blast.jpg" alt="" id="BLOGGER_PHOTO_ID_5340517639683898466" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Ohhhhhhhhhh......... I think something went wrong. Let me fix it. Here is the fixed output:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_VSqfPPWKu4Y/Sh1UuA5hABI/AAAAAAAAACs/FdQLiaLx-Mo/s1600-h/08.png"&gt;&lt;img style="cursor: pointer; width: 400px; height: 201px;" src="http://1.bp.blogspot.com/_VSqfPPWKu4Y/Sh1UuA5hABI/AAAAAAAAACs/FdQLiaLx-Mo/s400/08.png" alt="" id="BLOGGER_PHOTO_ID_5340517882623229970" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;That is for now. You can declare any type, enum, struct, class by this method. Yes, You can write a class AtomBomb and define a method Blast using Consume First.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-6020255705359563667?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/6020255705359563667/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/05/c-40-consume-first.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/6020255705359563667'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/6020255705359563667'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/05/c-40-consume-first.html' title='C# 4.0 Consume First'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_VSqfPPWKu4Y/Sh1RExLoGSI/AAAAAAAAABk/r8w44oOCrnI/s72-c/01.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-4359224100618879764</id><published>2009-05-26T11:44:00.002+06:00</published><updated>2009-05-26T12:48:27.027+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='IEnumerable'/><category scheme='http://www.blogger.com/atom/ns#' term='C# 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Interfaces'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 3.0'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='C# 3.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Visual Studio 2010 Beta 1'/><category scheme='http://www.blogger.com/atom/ns#' term='Generics'/><category scheme='http://www.blogger.com/atom/ns#' term='Covariance'/><title type='text'>C# 4.0 Generics and Variance</title><content type='html'>Yesterday I blogged about using the variance with delegates. Today I will expand that discussion to the Generics. In C# 3.0, arrays are co-variant, meaning array of a child class is also an array of its base class. e.g.&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;string&lt;/span&gt;[] astr = new &lt;span style="color: rgb(51, 51, 255);"&gt;string&lt;/span&gt;[] {"a", "b", "cde" };&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;object&lt;/span&gt;[] aobj = astr;&lt;br /&gt;There was no support for generic variance.&lt;br /&gt;C# 4.0 introduces covariance and contra variance. And changes the definition of &lt;span style="color: rgb(51, 153, 153);"&gt;IEnumerable&lt;/span&gt;&lt;t&gt; to &lt;span style="color: rgb(51, 153, 153);"&gt;IEnumerable&lt;/span&gt;&lt;&lt;span style="color: rgb(51, 51, 255);"&gt;out &lt;/span&gt;T&gt;. The &lt;&lt;span style="color: rgb(51, 51, 255);"&gt;out&lt;/span&gt; T&gt; specifies that if there is a method which takes &lt;span style="color: rgb(51, 153, 153);"&gt;Enumerable&lt;/span&gt;&lt;t&gt;, then the programmer can also pass the &lt;span style="color: rgb(51, 153, 153);"&gt;IEnumerable&lt;/span&gt;&lt;"Derived class of  T"&gt;. Consider this code:&lt;br /&gt;&lt;/t&gt;&lt;/t&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_VSqfPPWKu4Y/ShuAJfLTj2I/AAAAAAAAABY/hsyqWU266Lc/s1600-h/01.png"&gt;&lt;img style="cursor: pointer; width: 230px; height: 400px;" src="http://2.bp.blogspot.com/_VSqfPPWKu4Y/ShuAJfLTj2I/AAAAAAAAABY/hsyqWU266Lc/s400/01.png" alt="" id="BLOGGER_PHOTO_ID_5340002683653951330" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;PlayAll method takes an &lt;span style="color: rgb(51, 153, 153);"&gt;IEnumerable &lt;/span&gt;&lt;instrument&gt;and two calls to this method inside main work well even we pass &lt;/instrument&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;IEnumerable &lt;/span&gt;&lt;instrument&gt;&lt;piano&gt;and &lt;/piano&gt;&lt;/instrument&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;IEnumerable&lt;/span&gt;&lt;instrument&gt;&lt;piano&gt;&lt;guitar&gt;. Because &lt;/guitar&gt;&lt;/piano&gt;&lt;/instrument&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;IEnumerable &lt;/span&gt;&lt;instrument&gt;&lt;piano&gt;&lt;guitar&gt;is covariant, so it is legal to pass any derived types &lt;/guitar&gt;&lt;/piano&gt;&lt;/instrument&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;IEnumerable &lt;/span&gt;&lt;instrument&gt;&lt;piano&gt;&lt;guitar&gt;where a base class &lt;/guitar&gt;&lt;/piano&gt;&lt;/instrument&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;IEnumerable &lt;/span&gt;&lt;instrument&gt;&lt;piano&gt;&lt;guitar&gt;is required.&lt;br /&gt;&lt;/guitar&gt;&lt;/piano&gt;&lt;/instrument&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-4359224100618879764?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/4359224100618879764/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/05/c-40-generics-and-variance_26.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/4359224100618879764'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/4359224100618879764'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/05/c-40-generics-and-variance_26.html' title='C# 4.0 Generics and Variance'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_VSqfPPWKu4Y/ShuAJfLTj2I/AAAAAAAAABY/hsyqWU266Lc/s72-c/01.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-3520605041097741454</id><published>2009-05-25T12:09:00.004+06:00</published><updated>2009-05-25T12:21:28.997+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Delegates'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Covariance'/><category scheme='http://www.blogger.com/atom/ns#' term='Contra-variance'/><title type='text'>Delegate Covariance and Contra-variance in C# 4.0</title><content type='html'>C# 4.0 introduces co and contra-variance. Consider this code:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_VSqfPPWKu4Y/Sho3fduStNI/AAAAAAAAABI/paxAdhMb8N4/s1600-h/01.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 254px;" src="http://4.bp.blogspot.com/_VSqfPPWKu4Y/Sho3fduStNI/AAAAAAAAABI/paxAdhMb8N4/s400/01.jpg" alt="" id="BLOGGER_PHOTO_ID_5339641321895408850" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;The &lt;span style="color: rgb(51, 51, 255);"&gt;out &lt;/span&gt;and &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; keywords in the&lt;span style="color: rgb(51, 51, 255);"&gt; delegate &lt;/span&gt;definitions above specify the delegates to be co or contra variant. &lt;span style="color: rgb(51, 51, 255);"&gt;out&lt;/span&gt; keyword says that, Child class of T can be used instead of T in &lt;span style="color: rgb(51, 153, 153);"&gt;Func1 &lt;/span&gt;definition. While &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; keyword specifies that the base class of T can be used instead of T in &lt;span style="color: rgb(51, 153, 153);"&gt;Action1 &lt;/span&gt;definition.&lt;br /&gt;Try to play with this code, and try switching the places of &lt;span style="color: rgb(51, 51, 255);"&gt;out&lt;/span&gt; and &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; keywords in the above &lt;span style="color: rgb(51, 51, 255);"&gt;delegate&lt;/span&gt; definitions.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-3520605041097741454?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/3520605041097741454/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/05/delegate-covariance-and-contra-variance.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/3520605041097741454'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/3520605041097741454'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/05/delegate-covariance-and-contra-variance.html' title='Delegate Covariance and Contra-variance in C# 4.0'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_VSqfPPWKu4Y/Sho3fduStNI/AAAAAAAAABI/paxAdhMb8N4/s72-c/01.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-1109400333955779960</id><published>2009-05-24T20:22:00.000+06:00</published><updated>2009-05-24T20:40:31.608+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><category scheme='http://www.blogger.com/atom/ns#' term='dynamic'/><category scheme='http://www.blogger.com/atom/ns#' term='C# 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 3.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Anders Hejlsberg'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET Reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='C# 3.0'/><title type='text'>C# 4.0's new Keyword 'dynamic'</title><content type='html'>CSharp 4.0 introduces a new keyword &lt;span style="color: rgb(51, 51, 255);"&gt;dynamic&lt;/span&gt;. This keyword is introduced in C# to make interaction of CSharp with Dynamic Languages, JavaScript, Python and Ruby etc, easier. About the &lt;span style="color: rgb(51, 51, 255);"&gt;dynamic&lt;/span&gt; keyword, Anders Hejlsberg says "The &lt;span style="color: rgb(51, 51, 255);"&gt;dynamic &lt;/span&gt;keyword says that the static type of this variable is '&lt;span style="color: rgb(51, 51, 255);"&gt;dynamic&lt;/span&gt;'.". i.e. I don't know the type of this variable at compile time. So, leave it untill run time. But I can call any method defined on the type. These method calls would be made to the actual method defined. If that method is not defined, a run time exception would be thrown.&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;For example, we have a JavaScript calculator. We want to use that in C# 3.0 or below.  We will have to go to the .NET Reflection. Reflection is very slow mechanism. But with C# 4.0, we will write:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_VSqfPPWKu4Y/ShlcBSScMJI/AAAAAAAAAAU/hd5g-AvfjmI/s1600-h/01.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 193px;" src="http://1.bp.blogspot.com/_VSqfPPWKu4Y/ShlcBSScMJI/AAAAAAAAAAU/hd5g-AvfjmI/s400/01.jpg" alt="" id="BLOGGER_PHOTO_ID_5339400010383241362" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;GetJSCalculator() method will return the JavaScript calculator at run time. For more information visit these links:&lt;br /&gt;http://blogs.msdn.com/cburrows/archive/2008/10/27/c-dynamic.aspx&lt;br /&gt;http://blogs.msdn.com/samng/archive/2008/10/29/dynamic-in-c.aspx&lt;br /&gt;http://gsraj.tripod.com/dotnet/reflection.html&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-1109400333955779960?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/1109400333955779960/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/05/c-40s-new-keyword-dynamic.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/1109400333955779960'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/1109400333955779960'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/05/c-40s-new-keyword-dynamic.html' title='C# 4.0&apos;s new Keyword &apos;dynamic&apos;'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_VSqfPPWKu4Y/ShlcBSScMJI/AAAAAAAAAAU/hd5g-AvfjmI/s72-c/01.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-6293199301096218007</id><published>2009-05-23T18:44:00.000+06:00</published><updated>2009-05-24T20:43:15.186+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 3.0'/><category scheme='http://www.blogger.com/atom/ns#' term='CSharp 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='C# 3.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Namded Prameters'/><category scheme='http://www.blogger.com/atom/ns#' term='Complex'/><category scheme='http://www.blogger.com/atom/ns#' term='Optional Prameters'/><title type='text'>Optional and Namded Prameters in C# 4.0</title><content type='html'>C# 4.0 is coming with a lot of new features. Covariance, Contra-variance, dynamic programming, optional parameters etc. Today I'll talk about my most favorite feature, which was available in C++, optional parameters.&lt;br /&gt;We can use the optional parameters to reduce the number of method overloads. For example, upto C# 3.0 we had to write:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(51, 153, 153);"&gt;Complex&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);"&gt;private double &lt;/span&gt;real, imag;&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; Complex()&lt;br /&gt;        {&lt;br /&gt;                real = imag = 0.0;&lt;br /&gt;        }&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; Complex(double r)&lt;br /&gt;        {&lt;br /&gt;                real = r;&lt;br /&gt;                imag = 0.0;&lt;br /&gt;        }&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; Complex(double r, double img)&lt;br /&gt;        {&lt;br /&gt;                real = r;&lt;br /&gt;                imag = 0.0;&lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Now with C# 4.0, we can write above &lt;span style="color: rgb(51, 153, 153);"&gt;Complex&lt;/span&gt; class with a single constructor:&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(51, 153, 153);"&gt;Complex&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);"&gt;private double &lt;/span&gt;real, imag;&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; Complex(double r = 0.0, double img = 0.0)&lt;br /&gt;         {&lt;br /&gt;                 real = r;&lt;br /&gt;                imag = 0.0;&lt;br /&gt;         }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;But, with C# 4.0, a new gift comes free. Named Parameters, not available in C++, make the order of parameters irrelevant. Consider this Main method:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(51, 153, 153);"&gt;Program&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;        &lt;span style="color: rgb(51, 51, 255);"&gt;static void &lt;/span&gt;Main()&lt;br /&gt;          {&lt;br /&gt;          &lt;span style="color: rgb(51, 153, 153);"&gt;Complex &lt;/span&gt;c&lt;span style="color: rgb(51, 153, 153);"&gt;&lt;/span&gt;  = &lt;span style="color: rgb(51, 51, 255);"&gt;new &lt;/span&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;Complex&lt;/span&gt;(img:4.0);&lt;span style="color: rgb(51, 153, 153);"&gt;&lt;/span&gt;   &lt;span style="color: rgb(0, 153, 0);"&gt;//Pass imaginary value, real is 0.0 by defalult&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: rgb(51, 153, 153);"&gt;Complex &lt;/span&gt;c1&lt;span style="color: rgb(51, 153, 153);"&gt;&lt;/span&gt;  = &lt;span style="color: rgb(51, 51, 255);"&gt;new &lt;/span&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;&lt;/span&gt; &lt;span style="color: rgb(51, 153, 153);"&gt;Complex&lt;/span&gt;(r:4.0);&lt;span style="color: rgb(51, 153, 153);"&gt;&lt;/span&gt;   &lt;span style="color: rgb(0, 153, 0);"&gt;//Pass real value, imaginary is 0.0 by defalult&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;           Complex &lt;/span&gt;c2&lt;span style="color: rgb(51, 153, 153);"&gt;&lt;/span&gt;  = &lt;span style="color: rgb(51, 51, 255);"&gt;new &lt;/span&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;&lt;/span&gt; &lt;span style="color: rgb(51, 153, 153);"&gt;Complex&lt;/span&gt;();&lt;span style="color: rgb(51, 153, 153);"&gt;&lt;/span&gt;   &lt;span style="color: rgb(0, 153, 0);"&gt;//dont Pass any value, both are 0.0 by defalult&lt;/span&gt;&lt;br /&gt;          }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;The parameters names are from the method definition.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-6293199301096218007?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/6293199301096218007/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/05/c-4.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/6293199301096218007'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/6293199301096218007'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/05/c-4.html' title='Optional and Namded Prameters in C# 4.0'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2833886219840350519.post-3117099313061204378</id><published>2009-05-17T11:16:00.000+06:00</published><updated>2009-05-24T20:44:32.248+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='TemplateBinding'/><category scheme='http://www.blogger.com/atom/ns#' term='DrawingBrush'/><category scheme='http://www.blogger.com/atom/ns#' term='SolidColorBrush'/><category scheme='http://www.blogger.com/atom/ns#' term='RadialGradientBrush'/><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><category scheme='http://www.blogger.com/atom/ns#' term='VideoBrush'/><category scheme='http://www.blogger.com/atom/ns#' term='Brush'/><category scheme='http://www.blogger.com/atom/ns#' term='ControlTemplate'/><title type='text'>TemplateBinding in Silverlight</title><content type='html'>A week ago, or two, on silverlight forums there was a question regarding the TemplateBinding of background property to foreground. The question was:&lt;br /&gt;Can I bind background property to foreground property? I tried this but it didn't work:&lt;br /&gt;&lt;br /&gt;&amp;lt;ControlTemplate Key="btnTemplate" TargetType="Button"&amp;gt;&lt;br /&gt;     &amp;lt;Rectangle&amp;gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;&amp;lt;Rectangle.Fill&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt; &amp;lt;SolidColorBrush Color="TemplateBinding Background.Brush"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;  &amp;lt;/SolidColorBrush&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt; &amp;lt;/Rectangle.Fill&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/Rectangle&amp;gt;&lt;br /&gt;&amp;lt;/ControlTemplate&amp;gt;&lt;br /&gt;&lt;br /&gt;The Red Color code is the main problem. We can bind properties of same Type regardless of their name. The problem here is that, the type of Background is &lt;span style="color: rgb(0, 204, 204);"&gt;Brush &lt;/span&gt;while the programmer is specifying a concrete class &lt;span style="color: rgb(0, 204, 204);"&gt;SolidColorBrush&lt;/span&gt;. Background is not necessarily of type &lt;span style="color: rgb(0, 204, 204);"&gt;SolidColorBrush&lt;/span&gt;. It can be &lt;span style="color: rgb(0, 204, 204);"&gt;RadialGradientBrush&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;VideoBrush&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;DrawingBrush &lt;/span&gt;etc. Here is the right code:&lt;br /&gt;&lt;br /&gt;&amp;lt;ControlTemplate Key="btnTemplate" TargetType="Button"&amp;gt;&lt;br /&gt;     &amp;lt;Rectangle Fill="{TemplateBinding Background}" /&amp;gt;&lt;br /&gt;&amp;lt;/ControlTemplate&amp;gt;&lt;br /&gt;&lt;br /&gt;Moral:&lt;br /&gt;      Don't try to restrict your options to only &lt;span style="color: rgb(102, 204, 204);"&gt;SolidColorBrush &lt;/span&gt;if it provides you with the richness of other  &lt;span style="color: rgb(0, 204, 204);"&gt;Brush&lt;/span&gt;(es), also.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2833886219840350519-3117099313061204378?l=silverlight101.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://silverlight101.blogspot.com/feeds/3117099313061204378/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://silverlight101.blogspot.com/2009/05/week-ago-or-two-on-silverlight-forums.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/3117099313061204378'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2833886219840350519/posts/default/3117099313061204378'/><link rel='alternate' type='text/html' href='http://silverlight101.blogspot.com/2009/05/week-ago-or-two-on-silverlight-forums.html' title='TemplateBinding in Silverlight'/><author><name>Chemist</name><uri>http://www.blogger.com/profile/11199977276100215794</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://4.bp.blogspot.com/_VSqfPPWKu4Y/ShmTrdWbgQI/AAAAAAAAAAg/HwfMFS_ZwkM/S220/Image003.jpg'/></author><thr:total>0</thr:total></entry></feed>
