c# - using extension methods on int -
c# - using extension methods on int -
i'm reading extension methods, , monkeying around them see how work, , tried this:
namespace clunk { public static class oog { public static int doubleme(this int x) { homecoming 2 * x; } } class programme { static void main() { console.writeline(5.doubleme()); } } }
and worked expected, extending int doubleme method, printing 10.
next, beingness old c guy, wondered if this:
namespace clunk { public static class boollikec { public static bool operator true(this int i) { homecoming != 0; } public static bool operator false(this int i) { homecoming == 0; } } class programme { static void main() { if ( 7 ) { console.writeline("7 true"); } } } }
i think if former work, latter ought work create such int used in boolean context phone call extension method on int, check see 7 not equal 0, , homecoming true. instead, compiler doesn't later code, , puts reddish squiggly lines under 2 this's , says "type expected". why shouldn't work?
very clever! nice attempt, regrettably did not implement "extension everything", extension methods.
we considered implementing extension properties, extension operators, extension events, extension constructors, extension interfaces, name it, of them not compelling plenty create c# 4 or upcoming version of c#. got far designing syntax sort of thing mention. got rather farther on extension properties; almost got extension properties c# 4 ended not working out. sad story here.
http://blogs.msdn.com/b/ericlippert/archive/2009/10/05/why-no-extension-properties.aspx
so, long story short, no such feature, we'll consider hypothetical future releases of language.
you can of course of study create "tobool()" extension method on int if retro c convention non-zero-means-true.
c# extension-methods
Comments
Post a Comment