<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;DEcFRHk_fip7ImA9WhRRFE4.&quot;"><id>tag:blogger.com,1999:blog-8405432367240944722</id><updated>2011-11-27T18:20:15.746-05:00</updated><title>Random Code From The Software Jedi</title><subtitle type="html">Every couple days I'll publish some entertaining, and sometimes educational, cutting edge C# .NET stuff.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://thesoftwarejedi.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://thesoftwarejedi.blogspot.com/" /><author><name>Dana Hanna</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp0.blogger.com/_fEOxvrBATAY/Rtj205QryTI/AAAAAAAAS00/EuP17uk9rOg/s200/IMG_0087.JPG" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/RandomCodeFromTheSoftwareJedi" /><feedburner:info uri="randomcodefromthesoftwarejedi" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;CUYBSHc-cSp7ImA9WxRREk8.&quot;"><id>tag:blogger.com,1999:blog-8405432367240944722.post-3268425719392765041</id><published>2008-09-23T21:59:00.004-04:00</published><updated>2008-09-23T22:25:59.959-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-09-23T22:25:59.959-04:00</app:edited><title>ADO.NET Data Services + Oracle</title><content type="html">Wow - this is wicked cool stuff.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/cc668792.aspx"&gt;ADO.NET Data Services&lt;/a&gt; allow you to expose IQueryable objects as RESTful endpoints in ATOM, POX, and JSON.  Here's my brain dump of how cool this can be used for RAD purposes...&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/cc668792.aspx"&gt;ADO.NET Data Services&lt;/a&gt; is being plugged as just a endpoint for ADO.NET Entities, and thus just SQL Server's LINQ to SQL implementation.  Microsoft themselves are doing most of that damage - but I want to point out how it's much bigger than that!&lt;br /&gt;&lt;br /&gt;If you're like me, you have the pleasure (yes, pleasure) of working with Oracle instead of SQL Server.  Many times this leaves you feeling high and dry with all this cool new LINQ stuff.  There is no realy good LINQ to Oracle implementation out there - but the team at &lt;a href="http://www.hibernate.org/343.html"&gt;NHibernate&lt;/a&gt; has come to the rescue.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.hibernate.org/343.html"&gt;NHibernate&lt;/a&gt; is a .NET port of the years old Java Hibernate project - providing ORM for your software.  The importance of this framework in the RAD world is big - it allows you to very easily map a database to objects.&lt;br /&gt;&lt;br /&gt;LINQ to NHibernate is almost a year old now, and is in the process of being implemented in the main trunk of NHibernate (currently it's there, but throws a NotImplementedException after mocking you by showing you the SQL it knows that it should use).  However, in the NContrib trunk, I have found a &lt;a href="https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Linq/src/"&gt;quite functional version of LINQ to NHibernate&lt;/a&gt;!&lt;br /&gt;&lt;br /&gt;Why do I bring this up?  Because some kind people in the project have even gone so far as to implement the System.Data.Services.IUpdateable interface within the LINQ.NHibernate project.  This means fully functional &lt;a href="http://msdn.microsoft.com/en-us/library/cc668792.aspx"&gt;ADO.NET Data Services&lt;/a&gt; for your Oracle database.&lt;br /&gt;&lt;br /&gt;With only a couple lines of code, you can expose your &lt;a href="http://www.hibernate.org/343.html"&gt;NHibernate&lt;/a&gt; objects as queryable JSON, ATOM, POX RESTful services.  Simply extend &lt;a href="https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Linq/src/NHibernate.Linq/NHibernateContext.cs"&gt;NHibernateContext&lt;/a&gt;, provide a ProvideSession() implementation, and expose public IQueryable properties on your object.  Use this class as the type parameter of a new System.Data.Services.DataService&lt;t&gt; class, and you'll be all set.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/cc668792.aspx"&gt;ADO.NET data services&lt;/a&gt; provides the RESTful interface which is flexible enough to pass pagnation, ordering, filtering, etc straight through the web layer to the database layer.  So a javascript request for:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;http://server/someobject?$filter=someid eq '1'&amp;amp;$orderby=somefield&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;produces SQL sent straight to Oracle of&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;select *&lt;br /&gt;from someobject&lt;br /&gt;where someid = 1&lt;br /&gt;order by somefield&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;It's also fully updatable (through the IUpdateable implementation) which means that POST, PUT, DELETE requests work in a similar fashion.  It provides it in a JSON format, allowing you to use your database tables as javascript collections....  wow...  Again, the RAD implications are huge.&lt;/t&gt;&lt;div&gt;&lt;t&gt;&lt;br /&gt;The only issue that I have is with performance - &lt;a href="http://www.hibernate.org/343.html"&gt;NHibernate&lt;/a&gt; doesn't seem to stream resultsets (instead building the object array all at once), and the wordy ATOM/JSON formats don't suit some applications.  ADO.NET Data Services is NOT pluggable with custom serialization - I've decompiled and searched for an entry point for literally DAYS.&lt;br /&gt;&lt;br /&gt;One other point to make, is that referencing these new data services from VS2008 SP1 provides you with a client side LINQ object which translates linq queries to filtered HTTP requests (as in the filter/order/etc format above, which then translates to SQL...  So, cool.&lt;/t&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I'd provide some detailed examples and implementations - but that'd ruin all the fun ;)&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8405432367240944722-3268425719392765041?l=thesoftwarejedi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://thesoftwarejedi.blogspot.com/feeds/3268425719392765041/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8405432367240944722&amp;postID=3268425719392765041" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8405432367240944722/posts/default/3268425719392765041?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8405432367240944722/posts/default/3268425719392765041?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodeFromTheSoftwareJedi/~3/6nCN8jYmISc/adonet-data-services-astoria.html" title="ADO.NET Data Services + Oracle" /><author><name>Dana Hanna</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp0.blogger.com/_fEOxvrBATAY/Rtj205QryTI/AAAAAAAAS00/EuP17uk9rOg/s200/IMG_0087.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://thesoftwarejedi.blogspot.com/2008/09/adonet-data-services-astoria.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEICSHc6fyp7ImA9WxdRFUk.&quot;"><id>tag:blogger.com,1999:blog-8405432367240944722.post-3845008729616388009</id><published>2008-06-03T22:22:00.004-04:00</published><updated>2008-06-03T22:29:29.917-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-06-03T22:29:29.917-04:00</app:edited><title>More Linq - Find all methods having an attribute</title><content type="html">Not much time tonight, but here a little helper method that I wrote today.  It's just another example of my Linq addiction of late.&lt;br /&gt;&lt;br /&gt;I've had to search all loaded assemblies for methods or classes with certain custom attributes on them dozens of times when writing some generic framework for something or such...  So I linqified it today, and came up with this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public static List&amp;lt;MethodInfo&amp;gt;&lt;br /&gt;            GetMethodsWithAttribute(Type t, bool inherit)&lt;br /&gt;{&lt;br /&gt;    return (from ass in AppDomain.CurrentDomain&lt;br /&gt;                                       .GetAssemblies()&lt;br /&gt;             from c in ass.GetExportedTypes()&lt;br /&gt;             from m in c.GetMethods()&lt;br /&gt;             from a in m.GetCustomAttributes(t, inherit)&lt;br /&gt;             select m).ToList();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This sample just prints out all methods with the SecurityPermission attribute.  Notice my continued "abuse" of lambda as well...&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;GetMethodsWithAttribute(&lt;br /&gt;             typeof(SecurityPermissionAttribute), &lt;br /&gt;             true)&lt;br /&gt;    .ForEach((a) =&amp;gt;&lt;br /&gt;        Console.WriteLine("{0}.{1}", &lt;br /&gt;                a.DeclaringType.Name, &lt;br /&gt;                a.Name));&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/8405432367240944722-3845008729616388009?l=thesoftwarejedi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://thesoftwarejedi.blogspot.com/feeds/3845008729616388009/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8405432367240944722&amp;postID=3845008729616388009" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8405432367240944722/posts/default/3845008729616388009?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8405432367240944722/posts/default/3845008729616388009?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodeFromTheSoftwareJedi/~3/AV6QanuzLVA/more-linq-find-all-methods-having.html" title="More Linq - Find all methods having an attribute" /><author><name>Dana Hanna</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp0.blogger.com/_fEOxvrBATAY/Rtj205QryTI/AAAAAAAAS00/EuP17uk9rOg/s200/IMG_0087.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://thesoftwarejedi.blogspot.com/2008/06/more-linq-find-all-methods-having.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DE8ESHY9fyp7ImA9WxdRFU8.&quot;"><id>tag:blogger.com,1999:blog-8405432367240944722.post-5657360454669621189</id><published>2008-05-28T20:47:00.003-04:00</published><updated>2008-06-03T17:00:09.867-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-06-03T17:00:09.867-04:00</app:edited><title>LINQing the Fibonacci Sequence</title><content type="html">I've never been one for algorithms and/or academic type of questions/examples.  However, while interviewing for my latest job I was asked to write code to generate the &lt;a href="http://en.wikipedia.org/wiki/Fibonacci_number"&gt;Fibonacci sequence&lt;/a&gt;...  I struggled through it as much as my poor interviewing skills allowed.  It was only my second job interview ever (for my second job ever) so I guess that means I'm batting a 1.000, even if I suck at interviewing ;)&lt;br /&gt;&lt;br /&gt;Thinking of something to blog about tonight I figured I could make a FibonacciSequence generator that implemented IEnumerable just for shits and giggles (and I'll send the link to Johnny Mac for possible redemption).&lt;br /&gt;&lt;br /&gt;There are a dozen ways to generate the sequence (albeit in the interview I could barely find one - lol), but I chose this way because it easily allowed for the "yield" keyword.&lt;br /&gt;&lt;br /&gt;Also, then we can use LINQ over it...  Our final code can do this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;//an enumerator over every Fibonacci &lt;br /&gt;//number divisible by 3&lt;br /&gt;var qry = (from ulong i &lt;br /&gt;               in new FibonacciSequence()&lt;br /&gt;           where i % 3 == 0&lt;br /&gt;           select i);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;What's even better, is now I'm prepared for that next interview question, which could be "create an enumeration over a distinct ascending ordered list of every &lt;a href="http://en.wikipedia.org/wiki/Fibonacci_number"&gt;Fibonacci number &lt;/a&gt;divisible by 3 times every &lt;a href="http://en.wikipedia.org/wiki/Fibonacci_number"&gt;Fibonacci number &lt;/a&gt;divisible by 5 whose product is divisible by 17:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;var qry = (from ulong i &lt;br /&gt;               in new FibonacciSequence()&lt;br /&gt;           from ulong j&lt;br /&gt;               in new FibonacciSequence()&lt;br /&gt;           where j % 5 == 0 &lt;br /&gt;              &amp;&amp; i % 3 == 0&lt;br /&gt;           let res = (double)i*j&lt;br /&gt;           where res % 17 == 0&lt;br /&gt;           orderby res&lt;br /&gt;           select res).Distinct();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Amazingly, the above code will print out all elements of the enumeration in under 1 second.&lt;br /&gt;&lt;br /&gt;And finally, here's the FibonacciSequence class.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class FibonacciSequence : IEnumerable&amp;lt;ulong&amp;gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    public IEnumerator&amp;lt;ulong&amp;gt; GetEnumerator()&lt;br /&gt;    {&lt;br /&gt;        yield return 0;&lt;br /&gt;        yield return 1;&lt;br /&gt;        ulong a = 0;&lt;br /&gt;        ulong b = 0;&lt;br /&gt;        ulong c = 1;&lt;br /&gt;        checked&lt;br /&gt;        {&lt;br /&gt;            while (true)&lt;br /&gt;            {&lt;br /&gt;                a = b;&lt;br /&gt;                b = c;&lt;br /&gt;                try&lt;br /&gt;                {&lt;br /&gt;                    c = a + b;&lt;br /&gt;                }&lt;br /&gt;                catch (OverflowException)&lt;br /&gt;                {&lt;br /&gt;                    yield break;&lt;br /&gt;                }&lt;br /&gt;                yield return c;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    System.Collections.IEnumerator &lt;br /&gt;         System.Collections.IEnumerable.GetEnumerator()&lt;br /&gt;    {&lt;br /&gt;        return GetEnumerator();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As a final disclaimer - yes, the ulong to double conversion in the first example does result in a loss of precision, so please don't comment on that.&lt;br /&gt;&lt;br /&gt;Cheers!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8405432367240944722-5657360454669621189?l=thesoftwarejedi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://thesoftwarejedi.blogspot.com/feeds/5657360454669621189/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8405432367240944722&amp;postID=5657360454669621189" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8405432367240944722/posts/default/5657360454669621189?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8405432367240944722/posts/default/5657360454669621189?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodeFromTheSoftwareJedi/~3/W5Yb71_3Z-M/linqing-fibonacci-sequence.html" title="LINQing the Fibonacci Sequence" /><author><name>Dana Hanna</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp0.blogger.com/_fEOxvrBATAY/Rtj205QryTI/AAAAAAAAS00/EuP17uk9rOg/s200/IMG_0087.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://thesoftwarejedi.blogspot.com/2008/05/linqing-fibonacci-sequence.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CE8MRnczfSp7ImA9WxdSFE8.&quot;"><id>tag:blogger.com,1999:blog-8405432367240944722.post-2055895556892040799</id><published>2008-05-21T21:24:00.000-04:00</published><updated>2008-05-21T22:21:27.985-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-05-21T22:21:27.985-04:00</app:edited><title>Extension Methods</title><content type="html">I've been having a good time with extension methods in C# 3.0 lately.  You know, kids to bed, having a beer, reading digg.com and thinking, "What am I doing?!?!  I wanna write some extension methods!  Hell Yes!!!" &amp;lt;/sarcasm&amp;gt; &lt;br /&gt;&lt;br /&gt;If you don't know about, or understand extension methods - google it.  This post is NOT for the beginning developer.  I'm not providing complete examples that you can just pop in your code.  You need to understand where to put these extension methods (in some static class in a special namespace that isn't ALWAYS included).  I'm not catering to the script kiddies here (script kiddies: go back to the IRC #hak5 chat room please).&lt;br /&gt;&lt;br /&gt;The first thing everyone seems to want to do is take all of the static methods from the static "Convert" class and apply them to the appropriate objects.  Certainly a reasonable approach.  However, instead of writing it all out, we can just codegen all of the methods.  The Jedi is lazy - most of you know that already.&lt;br /&gt;This code will popup a messagebox that you can copy the code for all 100+ (I'm not counting) conversion methods.  The end result is that all primitives have a "ToWhatever()" method that makes it easy to do conversions.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;static void Main()&lt;br /&gt;{&lt;br /&gt;   StringBuilder code = new StringBuilder();&lt;br /&gt;   foreach (var m in typeof(Convert).GetMethods(&lt;br /&gt;                           BindingFlags.Public |&lt;br /&gt;                           BindingFlags.Static))&lt;br /&gt;   {&lt;br /&gt;       ParameterInfo[] parameters =&lt;br /&gt;                               m.GetParameters();&lt;br /&gt;       code.AppendFormat("public static {0} {1}(",&lt;br /&gt;              m.ReturnParameter.ParameterType.Name,&lt;br /&gt;              m.Name);&lt;br /&gt;       bool first = true;&lt;br /&gt;       foreach (var p in parameters)&lt;br /&gt;       {&lt;br /&gt;           if (first)&lt;br /&gt;           {&lt;br /&gt;               code.Append("this ");&lt;br /&gt;               first = false;&lt;br /&gt;           }&lt;br /&gt;           code.AppendFormat("{0} {1}, ",&lt;br /&gt;                           p.ParameterType.Name,&lt;br /&gt;                           p.Name);&lt;br /&gt;       }&lt;br /&gt;       code.Remove(code.Length - 2, 2);&lt;br /&gt;       code.AppendFormat(") {{ return Convert.{0}(",&lt;br /&gt;                                  m.Name);&lt;br /&gt;       foreach (var p in m.GetParameters())&lt;br /&gt;       {&lt;br /&gt;           code.AppendFormat("{0}, ", p.Name);&lt;br /&gt;       }&lt;br /&gt;       code.Remove(code.Length - 2, 2);&lt;br /&gt;       code.Append("); }");&lt;br /&gt;       code.AppendLine();&lt;br /&gt;   }&lt;br /&gt;   MessageBox.Show(code.ToString());&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Sweet...  But, that code will throw on exceptions...  What if we wanted a "TryParse" type of equivalent for every single one?  Time for another approach.&lt;br /&gt;All of these objects are IConvertible....  Hmmmm...  how about this approach!&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public static T To&amp;lt;T&amp;gt;(this IConvertible obj)&lt;br /&gt;{&lt;br /&gt;  return (T)Convert.ChangeType(obj, typeof(T));&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Sweet!  Now we can do&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;int i = myString.To&amp;lt;int&amp;gt;();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So adding a couple helper methods is easy as pie.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public static T ToOrDefault&amp;lt;T&amp;gt;&lt;br /&gt;             (this IConvertible obj)&lt;br /&gt;{&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;        return To&amp;lt;T&amp;gt;(obj);&lt;br /&gt;    }&lt;br /&gt;    catch&lt;br /&gt;    {&lt;br /&gt;        return default(T);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static bool ToOrDefault&amp;lt;T&amp;gt;&lt;br /&gt;                    (this IConvertible obj,&lt;br /&gt;                     out T newObj)&lt;br /&gt;{&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;        newObj = To&amp;lt;T&amp;gt;(obj); &lt;br /&gt;        return true;&lt;br /&gt;    }&lt;br /&gt;    catch&lt;br /&gt;    {&lt;br /&gt;        newObj = default(T); &lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static T ToOrOther&amp;lt;T&amp;gt;&lt;br /&gt;                       (this IConvertible obj,&lt;br /&gt;                       T other)&lt;br /&gt;{&lt;br /&gt;  try&lt;br /&gt;  {&lt;br /&gt;      return To&amp;lt;T&amp;gt;obj);&lt;br /&gt;  }&lt;br /&gt;  catch&lt;br /&gt;  {&lt;br /&gt;      return other;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static bool ToOrOther&amp;lt;T&amp;gt;&lt;br /&gt;                         (this IConvertible obj,&lt;br /&gt;                         out T newObj,&lt;br /&gt;                         T other)&lt;br /&gt;{&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;        newObj = To&amp;lt;T&amp;gt;(obj);&lt;br /&gt;        return true;&lt;br /&gt;    }&lt;br /&gt;    catch&lt;br /&gt;    {&lt;br /&gt;        newObj = other;&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static T ToOrNull&amp;lt;T&amp;gt;&lt;br /&gt;                      (this IConvertible obj)&lt;br /&gt;                      where T : class&lt;br /&gt;{&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;        return To&amp;lt;T&amp;gt;(obj);&lt;br /&gt;    }&lt;br /&gt;    catch&lt;br /&gt;    {&lt;br /&gt;        return null;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static bool ToOrNull&amp;lt;T&amp;gt;&lt;br /&gt;                  (this IConvertible obj,&lt;br /&gt;                  out T newObj)&lt;br /&gt;                  where T : class&lt;br /&gt;{&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;        newObj = To&amp;lt;T&amp;gt;(obj);&lt;br /&gt;        return true;&lt;br /&gt;    }&lt;br /&gt;    catch&lt;br /&gt;    {&lt;br /&gt;        newObj = null;&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now we can ask for default (calls blank constructor or "0" for numerics) on failure, specify a "default" value (I call it "other"), or ask for null (where T : class).  I've also provided both silent exception models, and a typical TryParse model that returns a bool indicating the action taken, and an out param holds the new value.&lt;br /&gt;So our code can do things like this&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;string a = myInt.ToOrDefault&amp;lt;string&amp;gt;();&lt;br /&gt;//note type inference&lt;br /&gt;DateTime d = myString.ToOrOther(DateTime.MAX_VALUE);&lt;br /&gt;double d;&lt;br /&gt;//note type inference&lt;br /&gt;bool didItGiveDefault = myString.ToOrDefault(d);&lt;br /&gt;string s = myDateTime.ToOrNull&amp;lt;string&amp;gt;();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Oddly type inference doesn't pickup on expected return types.  I'd think it would.  Thoughts?&lt;br /&gt;&lt;br /&gt;So - I'd think that's about the best you can get.  I couldn't get Nullable types to roll into the whole thing very cleanly.  I tried for about 20 minutes before I threw in the towel.&lt;br /&gt;&lt;br /&gt;Enjoy!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8405432367240944722-2055895556892040799?l=thesoftwarejedi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://thesoftwarejedi.blogspot.com/feeds/2055895556892040799/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8405432367240944722&amp;postID=2055895556892040799" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8405432367240944722/posts/default/2055895556892040799?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8405432367240944722/posts/default/2055895556892040799?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodeFromTheSoftwareJedi/~3/KubpZ0vPnms/extension-methods.html" title="Extension Methods" /><author><name>Dana Hanna</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp0.blogger.com/_fEOxvrBATAY/Rtj205QryTI/AAAAAAAAS00/EuP17uk9rOg/s200/IMG_0087.JPG" /></author><thr:total>3</thr:total><feedburner:origLink>http://thesoftwarejedi.blogspot.com/2008/05/extension-methods.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkEHSX8_fyp7ImA9WxdTGE8.&quot;"><id>tag:blogger.com,1999:blog-8405432367240944722.post-5184494057810404757</id><published>2008-05-14T21:10:00.000-04:00</published><updated>2008-05-14T23:03:58.147-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-05-14T23:03:58.147-04:00</app:edited><title>4 naughty C# 3.5 code "changes" for your last day at work</title><content type="html">&lt;span style="font-size:85%;"&gt;I recently left my job of over 8 years, and it got me to thinking about the fun I could have had with the millions of lines of code that I had access to.  Of course, I didn't do any of these things (though I wish I had done some of the funny ones), and I won't be doing them in the future at my new job either.  Let me clearly state that this post is meant to be read as humor.  However, it &lt;/span&gt;&lt;span style="font-weight: bold;font-size:85%;" &gt;is &lt;/span&gt;&lt;span style="font-size:85%;"&gt;meant to be informative regarding .NET 3.5 / C# 3.0.&lt;br /&gt;&lt;br /&gt;Leaving your job?  Being laid off?  Here's 4 fun/annoying/destructive things to leave behind!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;4. Refactor random methods into operator overloads&lt;br /&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;(rated: annoying)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Find some methods throughout the code that match operator parameter lists and replace them with operator overloads.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;change this:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;public void MyMethod()&lt;/span&gt; &lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;{&lt;/span&gt; &lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;//does something&lt;/span&gt; &lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;}&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-size:85%;"&gt;into this:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;public static int operator ~ (TheClass c)&lt;/span&gt; &lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;{&lt;/span&gt; &lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    //do the same thing referencing "c" instead of "this"&lt;/span&gt; &lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;}&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-size:85%;"&gt;or change this:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;public string MyOtherMethod(string something)&lt;/span&gt;  &lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;{&lt;/span&gt;  &lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;//does something&lt;/span&gt;  &lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;}&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-size:85%;"&gt;into this:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;public static string operator + (TheClass c, string something)&lt;/span&gt;  &lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;{&lt;/span&gt;  &lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;//do the same thing referencing "c" instead of "this"&lt;/span&gt;  &lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-size:85%;"&gt;Then go through all the broken code and adjust &lt;/span&gt;&lt;span style="font-size:85%;"&gt;it to use the overloads instead of the methods.  This would make the code TERRIBLY confusing!&lt;br /&gt;&lt;br /&gt;I&lt;/span&gt;&lt;span style="font-size:85%;"&gt;magine seeing:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;MyClass&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt; m = new MyClass();&lt;br /&gt;~m;&lt;br /&gt;string answer = m + "Jedi";&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span&gt;&lt;span style="font-size:85%;"&gt;Greatest part about this annoyance is that "Find References" doesn't work on overloads, so beginner programmers are left stumped;  Especially with the "~" overload as many people have never even seen that before outside of destructors!&lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:130%;"&gt;3. Create Extension methods with hilarious names on base objects&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;rated: funny&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Find some obscure cs file deep within the code, and add this at the bottom:&lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;namespace System&lt;br /&gt;{&lt;br /&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;public static class HaHa&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;public static void ThatsWhatYourMotherSaid(this object o)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;{&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-style: italic;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Now every object in the entire codebase has a useless method called "&lt;/span&gt;&lt;span style="font-size:85%;"&gt;ThatsWhatYourMotherSaid".  Maybe I'm a geek, but that's hilarious.  Note: this is easily findable with a "Go to definition" context option.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-size:85%;" &gt;If you'd rather be annoying instead of funny&lt;/span&gt;&lt;span style="font-size:85%;"&gt;, do the same thing, but write a program to generate code creating methods for every English word in the dictionary.  That would render Intellisense useless - and possibly even cause major IDE slowdown (I didn't try!).&lt;br /&gt;&lt;br /&gt;This works well because we put the class "HaHa" in the namespace "System".  This namespace is included in every C# file ("using System;"), unless it's been strangely removed.  Extension methods are only revealed on objects if the namespace in which they are defined is included (yeah, that's why you have "using System.Linq;" in your code!).&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;2. Create seemingly useful extension methods which do nothing&lt;/span&gt;, &lt;span style="font-size:130%;"&gt;or worse...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;rated: annoying at the least, possibly destructive&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;br /&gt;Once again, find some obscure file to hide this in.  Similar to above, create an extension method but this time make it sound useful.  New people, or beginners joining the project may think, "Wow! That's great!".  Here's some examples I can think of:&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-size:85%;"&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-size:85%;"&gt;namespace System&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;public static class HaHa&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;public static bool EqualsCaseInsensitive(this string s1, string s2)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;{&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;return true;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;public static void ShowOnTop(this Form f)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;f.Close()&lt;/span&gt;;&lt;br /&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;    &lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-style: italic;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;Now your strings have a "EqualsCaseInsensitive" method which always returns true, and your forms have a "ShowOnTop" method that closes the form (which also calls Dispose() I believe)!  Evil....&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;1. Replace a widely used core library class with a creatively destructive decorator class&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;rated: Extremely destructive&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;br /&gt;Creating a decorator (aka wrapper) class for a core framework class which has the exact same name will allow existing code to compile, but you gain control over it's behavior.  The issue here is that if you define a class in local code under the same namespace as a framework class, it will be used instead of the framework class.  Here's an INCOMPLETE example:&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;namespace System.Threading&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;    public class Thread&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        private global::System.Threading.Thread _wrappedThread;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        public Thread(ThreadStart start)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;        {&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;        &lt;/span&gt;&lt;span style="font-style: italic;"&gt;    _wrappedThread = new global::System.Threading.Thread(start);&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        .&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        .&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        .&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        public void Start()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;            if (new Random().Next(1, 101) &gt;= 99)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;            {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;                return;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;            else&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;            {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;                _wrappedThread.Start();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        }&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;        .&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;        .&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;        .&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;To make this example complete, you'd have to decorate every method, property, and event on the Thread class.  Tedious, but not too bad with a program to generate the code.  Note the usage of "global::" which makes it use the REAL Thread class.&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-size:85%;" &gt;Bottom line is, with the above example (if completed), you'd end up with System.Threading.Thread.Start() only starting 98% of the time.  Now THAT is a bitch!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;Cheers,&lt;br /&gt;The Software Jedi&lt;/span&gt;&lt;span style="font-weight: bold;font-size:85%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8405432367240944722-5184494057810404757?l=thesoftwarejedi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://thesoftwarejedi.blogspot.com/feeds/5184494057810404757/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8405432367240944722&amp;postID=5184494057810404757" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8405432367240944722/posts/default/5184494057810404757?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8405432367240944722/posts/default/5184494057810404757?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodeFromTheSoftwareJedi/~3/szKUTh9403E/4-naughty-c-35-code-changes-for-your.html" title="4 naughty C# 3.5 code &quot;changes&quot; for your last day at work" /><author><name>Dana Hanna</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp0.blogger.com/_fEOxvrBATAY/Rtj205QryTI/AAAAAAAAS00/EuP17uk9rOg/s200/IMG_0087.JPG" /></author><thr:total>1</thr:total><feedburner:origLink>http://thesoftwarejedi.blogspot.com/2008/05/4-naughty-c-35-code-changes-for-your.html</feedburner:origLink></entry></feed>

