Extention on a Lambda function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael S

    Extention on a Lambda function?

    Is extentions only allowed on 'pure' methods?

    I was playing arround with lambda functions and while googling I tried a
    small example (In a static class):


    public static Func<double, double, doubleHypotenus e = (x,y) =>
    Math.Sqrt(x*x + y*y);


    Now it would be cool if I could easily make it an extention on double, like:
    public static Func<double, double, double, doubleHypotenus e = (this t,
    x,y) =Math.Sqrt(x*x + y*y);

    Is this even possible or am I just lost on syntax?

    Regards

    - Michael S




  • Michael S

    #2
    Re: Extention on a Lambda function?


    "Michael S" <no@no.nowrot e in message
    news:ORuExt6WHH A.1432@TK2MSFTN GP02.phx.gbl...

    Was this question to hard, non-understandable or simply stupid?

    Help me Jon-Skeet-Kenobi, your are my only hope.. =)

    - Michael S


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Extention on a Lambda function?

      Michael S <no@no.nowrot e:
      "Michael S" <no@no.nowrot e in message
      news:ORuExt6WHH A.1432@TK2MSFTN GP02.phx.gbl...
      >
      Was this question to hard, non-understandable or simply stupid?
      >
      Help me Jon-Skeet-Kenobi, your are my only hope.. =)
      LOL. Unfortunately I haven't looked at C# 3.0 enough recently to know
      for sure whether or not you could do it, but I don't see why you
      shouldn't be able to put an extension on System.Double. On the other
      hand, your method didn't use the "this" part, so I'm not sure how
      useful it would be...

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Oliver Sturm

        #4
        Re: Extention on a Lambda function?

        Hello Michael,
        >Is this even possible or am I just lost on syntax?
        I made some tests with the following implementation:

        class Program {
        static void Main(string[] args) {
        Console.WriteLi ne("Hi".AddSome thing(" there"));
        Console.WriteLi ne(Helper.AddSo methingLambda(" Hi", " there"));

        Console.ReadLin e();
        }

        public static class Helper {
        public static string AddSomething(th is string val, string addOn) {
        return val + addOn;
        }

        public static Func<string,str ing,stringAddSo methingLambda =
        (val,addOn) =val + addOn;
        }
        }

        I don't find a way to use the "this" keyword in the "AddSomethingLa mbda"
        declaration that the compiler likes. Seems to me this doesn't work... it's
        probably rather logical - extension methods work, afaik, because the
        compiler converts the call into the "standard" syntax of a call to
        Helper.XXX at compile time. While my example doesn't show this, it would
        theoretically be possible that my AddSomethingLam bda didn't even contain
        anything - it's a runtime assignment, not something that can be evaluated
        at compile time.


        Oliver Sturm
        --

        Comment

        Working...