Extention on a Lambda function?

  • Thread starter Thread starter Michael S
  • Start date Start date
M

Michael S

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, double> Hypotenuse = (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, double> Hypotenuse = (this t,
x,y) => Math.Sqrt(x*x + y*y);

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

Regards

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

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

- Michael S
 
Michael S said:
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...
 
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.WriteLine("Hi".AddSomething(" there"));
Console.WriteLine(Helper.AddSomethingLambda("Hi", " there"));

Console.ReadLine();
}

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

public static Func<string,string,string> AddSomethingLambda =
(val,addOn) => val + addOn;
}
}

I don't find a way to use the "this" keyword in the "AddSomethingLambda"
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 AddSomethingLambda didn't even contain
anything - it's a runtime assignment, not something that can be evaluated
at compile time.


Oliver Sturm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top