Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using extension methods on int

I'm reading about extension methods, and monkeying around with them to see how they work, and I tried this:

namespace clunk {
    public static class oog {
        public static int doubleMe(this int x) {
            return 2 * x;
        }
    }

    class Program {
        static void Main() {
            Console.WriteLine(5.doubleMe());
        }
    }
}

and it worked as expected, successfully extending int with the doubleMe method, printing 10.

Next, being an old C guy, I wondered if I could do this:

namespace clunk {
    public static class BoolLikeC {
        public static bool operator true(this int i)  { return i != 0; }
        public static bool operator false(this int i) { return i == 0; }
    }

    class Program {
        static void Main() {
            if ( 7 ) {
                Console.WriteLine("7 is so true");
            }
        }
    }
}

I would think if the former would work, then the latter ought to work to make it such that an int used in a boolean context would call the extension method on int, check to see that 7 is not equal to 0, and return true. But instead, the compiler doesn't even like the later code, and puts the red squiggly lines under the two this's and says "Type expected". Why shouldn't this work?

like image 748
Dave Avatar asked Jul 11 '11 21:07

Dave


People also ask

How do you add an extension to an integer?

Unfortunately you cannot use extension methods to add operators, and implicit type conversion in C# is implemented as an operator. Show activity on this post. In the first instance you are writing an extension method - e.g. extending the functionality of the int data type.

Can you explain extension methods and write one for int data type?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

Should you use extension methods?

Extension methods are an excellent addition to the C# language. They enable us to write nicer, more readable code. They allow for more functionally styled programming, which is very much needed in an object-oriented language. They also should be used with care.

What parameter does an extend () method requires?

Parameters of extend() function in Python The list extend() method has only one parameter, and that is an iterable. The iterable can be a list, tuple (it is like an immutable list), string, set or, even a dictionary.


1 Answers

Very clever! A nice attempt, but regrettably we did not implement "extension everything", just extension methods.

We considered implementing extension properties, extension operators, extension events, extension constructors, extension interfaces, you name it, but most of them were not compelling enough to make it into C# 4 or the upcoming version of C#. We got as far as designing a syntax for the sort of thing you mention. We got rather farther on extension properties; we almost got extension properties into C# 4 but it ended up not working out. The sad story is here.

http://blogs.msdn.com/b/ericlippert/archive/2009/10/05/why-no-extension-properties.aspx

So, long story short, no such feature, but we'll consider it for hypothetical future releases of the language.

You can of course make a "ToBool()" extension method on int if you really do like the retro C convention that non-zero-means-true.

like image 157
Eric Lippert Avatar answered Nov 09 '22 23:11

Eric Lippert