<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx</link><description>Lets consider I have a class Employee which has Name and JobGrade fields. I want to overload the comparison operators for this class so that it can participate in all types of comparison including &amp;lt; . &amp;lt;= , == , &amp;gt;= , != and Equals . I want to</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#479612</link><pubDate>Tue, 11 Oct 2005 17:56:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:479612</guid><dc:creator>Hasani</dc:creator><description>space ship operator: This is something that should defintiely be implemented in c# but I think the compiler should unconditionaly create 2 3 and 4.</description></item><item><title>re: C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#479852</link><pubDate>Wed, 12 Oct 2005 01:43:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:479852</guid><dc:creator>Damien Guard</dc:creator><description>This got me thinking and I've knocked up a class that implements this for you providing you can inherit from it.&lt;br&gt;&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://damieng.blogspot.com/2005/10/automatic-comparison-operator.html"&gt;http://damieng.blogspot.com/2005/10/automatic-comparison-operator.html&lt;/a&gt;</description></item><item><title>re: C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#480015</link><pubDate>Wed, 12 Oct 2005 13:15:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:480015</guid><dc:creator>Jesse McGrew</dc:creator><description>Yes, Perl has a spaceship operator. (Also 'cmp', the spaceship equivalent for strings, to go with 'eq', 'lt', etc.)</description></item><item><title>re: C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#491801</link><pubDate>Fri, 11 Nov 2005 18:58:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491801</guid><dc:creator>Richard Corfield</dc:creator><description>Just had geat fun with this and Generics. &lt;br&gt;&lt;br&gt;Consider the simple methods&lt;br&gt;&lt;br&gt;public bool ArrayContains&amp;lt;T&amp;gt;(T[] array, T value) where T:class&lt;br&gt;{&lt;br&gt;   return array != null &amp;amp;&amp;amp;&lt;br&gt;     Array.Exists(array, delegate(candidate) {&lt;br&gt;         return candidate == value;&lt;br&gt;     });&lt;br&gt;}&lt;br&gt;&lt;br&gt;public void ExcludeAll&amp;lt;T&amp;gt;(ref T[] array,&lt;br&gt;                          T value) where T:class&lt;br&gt;{&lt;br&gt;   List&amp;lt;T&amp;gt; list = new List&amp;lt;T&amp;gt;();&lt;br&gt;   if(array != null)&lt;br&gt;   {&lt;br&gt;       foreach(T t in array)&lt;br&gt;       {&lt;br&gt;           if (t != value) list.Add(t);&lt;br&gt;       }&lt;br&gt;       array = t.ToArray();&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;br&gt;That &amp;quot;where T:class&amp;quot; raised alarm bells. The code won't compile without it, but the method as given should surely work if T is an int?&lt;br&gt;&lt;br&gt;Now consider the case where T is string. The code fails. I think the reason is that because the &amp;quot;operator ==&amp;quot; implementations are defined as requiring the variables to be known to be string at compile time. This is not resolved at JIT time or whenever string gets substituded for T.&lt;br&gt;&lt;br&gt;Based on your code above, I'd expect the following to fail:&lt;br&gt;&lt;br&gt;   object a = &amp;quot;Hello&amp;quot;;&lt;br&gt;   object b = &amp;quot;Hello&amp;quot;;&lt;br&gt;   bool shouldBeTrue = (a == b);&lt;br&gt;&lt;br&gt;where&lt;br&gt;&lt;br&gt;   string a = &amp;quot;Hello&amp;quot;;&lt;br&gt;   string b = &amp;quot;Hello&amp;quot;;&lt;br&gt;   bool isTrueThisTime = (a == b);&lt;br&gt;&lt;br&gt;because the operator overload workes like &amp;quot;new&amp;quot; in place of &amp;quot;override&amp;quot;. The variable has to be the right type. It's not simple polymorphism where only the type of the object pointed to counts. I wanted polymorphic behaviour so using .Equals instead makes more sense.&lt;br&gt;&lt;br&gt;The solution for my code problem, after much fighting a very upset debugger, was to replace == with .Equals(). All in all, a subtle problem. &lt;br&gt;&lt;br&gt;Want polymorphic equality checking? Use .Equals(). &lt;br&gt;&lt;br&gt;Want non-polymorphic equality checking use ==.&lt;br&gt;</description></item><item><title>re: C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#506127</link><pubDate>Wed, 21 Dec 2005 01:37:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:506127</guid><dc:creator>Eileen</dc:creator><description>I bet it's possible come up with a snippet that would fill out the first example for you (minus the Compare method), instead of waiting for the compiler to support it.</description></item><item><title>re: C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#539737</link><pubDate>Mon, 27 Feb 2006 11:04:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:539737</guid><dc:creator>Abhi Win</dc:creator><description>Hi, Nice article.&lt;br&gt;&lt;br&gt;Just tell me thus this method (CompareTo(object)) is called from the external sorting method too for sorting items of our class ?? For ex: In datagrid auto-sorting case???&lt;br&gt;&lt;br&gt;Thanks&lt;br&gt;Abhi Win&lt;br&gt;abhi.win@gmail.com</description></item><item><title>re: C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#599363</link><pubDate>Wed, 17 May 2006 01:33:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:599363</guid><dc:creator>Brian Fink</dc:creator><description>I think the reason why a spaceship operator is not included in c#, for your particular implementation at least, is that stepping through a list of truth tests until you find one that matches your case robs the program of performance. On the other hand, if each case of the comparison operator is overloaded individually, the only code called will be the code for the particular comp op that you specified, providing much cleaner execution. True, the required code is more verbose, but that's the price we pay sometimes for speed.</description></item><item><title>re: C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#604140</link><pubDate>Tue, 23 May 2006 00:19:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:604140</guid><dc:creator>Brian Fink</dc:creator><description>Here's a way to implement the &amp;quot;spaceship&amp;quot; algorithm that's intuitive rather than exhaustively comparative:&lt;br&gt;&lt;br&gt;public static int operator &amp;lt;=&amp;gt;(Employee emp1, Employee emp2)&lt;br&gt;{&lt;br&gt; return (emp2.JobGrade&amp;lt;emp1.JobGrade)-(emp1.JobGrade&amp;lt;emp2.JobGrade);&lt;br&gt;}&lt;br&gt;&lt;br&gt;Returns the same values, but only makes two comparisons.</description></item><item><title>re: C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#685192</link><pubDate>Tue, 01 Aug 2006 15:25:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:685192</guid><dc:creator>Mike </dc:creator><description>What happen with your code when you do something like this:&lt;br&gt;if( myEmployee == null)&lt;br&gt;{&lt;br&gt; &amp;nbsp; .....&lt;br&gt;}&lt;br&gt;&lt;br&gt;this is the same as calling &lt;br&gt;Comparison(myEmployee, null). I think this will crash when it tries to do emp2.JobGrade because emp2 is null. &lt;br&gt;</description></item><item><title>handling nulls</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#3150706</link><pubDate>Fri, 08 Jun 2007 02:59:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3150706</guid><dc:creator>Travis Johnson</dc:creator><description>&lt;p&gt;Inserting the following at the top of the Comparison function will handle the null issue (or anyone else stumbling across this via google)...&lt;/p&gt;
&lt;p&gt;	if ((object)day1 == null &amp;amp;&amp;amp; (object)day2 == null) return 0;&lt;/p&gt;
&lt;p&gt;	else if ((object)day1 == null) return -1;&lt;/p&gt;
&lt;p&gt;	else if ((object)day2 == null) return 1;&lt;/p&gt;
&lt;p&gt;...note, casting to &amp;quot;object&amp;quot; is done to avoid infinite recursion (else they would continue to call the same comparison operators, and an object cast works just fine for testing against null).&lt;/p&gt;
</description></item><item><title>  Automatic comparison operator overloading in C# at  DamienG</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#4534001</link><pubDate>Fri, 24 Aug 2007 04:11:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4534001</guid><dc:creator>  Automatic comparison operator overloading in C# at  DamienG</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://damieng.com/blog/2005/10/11/automaticcomparisonoperatoroverloadingincsharp"&gt;http://damieng.com/blog/2005/10/11/automaticcomparisonoperatoroverloadingincsharp&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#9487265</link><pubDate>Wed, 18 Mar 2009 19:41:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9487265</guid><dc:creator>Tom</dc:creator><description>&lt;p&gt;Really, you only need to have this:&lt;/p&gt;
&lt;p&gt;return emp1.JobGrade &amp;lt;=&amp;gt; emp2.JobGrade;&lt;/p&gt;
&lt;p&gt;No need for all those else ifs.&lt;/p&gt;
</description></item><item><title>re: C#: Comparison operator overloading and spaceship operator</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/11/479537.aspx#9921690</link><pubDate>Fri, 13 Nov 2009 01:30:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9921690</guid><dc:creator>Bala Balaji Raju</dc:creator><description>&lt;p&gt;Hey can you tell me how to compare two list objects in c#.net&lt;/p&gt;
</description></item></channel></rss>