CSharpFeeds - All your C# feeds in one place.

Sponsors

Friday, June 15, 2007

Overloading == to return a non-boolean

by skeet via Jon Skeet's Coding Blog : C# on 6/15/2007 5:38:42 AM

Were you aware that you could overload == to return types other than boolean? I certainly wasn't until I started reading through the lifted operators part of the C# 2 specification. It's quite bizarre - here it is in action:

using System;

class Test
{
    public static string operator== (Test t1, Test t2)
    {
        return "Fish?";
    }
    
    public static string operator!= (Test t1, Test t2)
    {
        return "Not a fish?";
    }

    static void Main()
    {
        Test a = new Test();
        Test b = new Test();
        Console.WriteLine (a==b);
    }
}

That ends up printing "Fish?" to the console. Strange but true. When I asked about this on the newsgroup, one poster said that he did use this functionality for an ORM type of system - his == operator on two expressions would return another expression which represented the test for equality between the other two expressions. I can't say I much like this idea, although I could see where he was coming from. (The C# spec does specifically discourage this sort of thing, which is at least a start).

So, dear readers, have any of you done this, and if so, why?

email it!bookmark it!digg it!

Original Post: Overloading == to return a non-boolean

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us