2

I usually need to write custom lambda expressions during debugging.

As an example:

(new Func<int, string>(x => $"{commandParameters[x].ParameterName} : {commandParameters[x].Value}"))(2)

or a shorter one:

(new Func<int, string>(x => x.ToString()))(2)

or

((Func<int, string>)(x => x.ToString()))(2)

I'd like to know the simplest form that we can write such expressions so that they can be evaluated in Watch window.

Omitting the Func construction we would have (x => x.ToString())(2) which is much more readable but it won't be evaulated due to CS0149: Method name expected

Lovely Candidates:

  • (x => x.ToString())(2)
  • ((int x) => x.ToString())(2)
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
  • How about a type cast, instead of `new`? `(Func)(x => x.ToString())(2)` – Zev Spitz Jul 26 '17 at 06:17
  • @ZevSpitz, I already added it in edited question, it's simpler but yet we have to specify the func type – Mohsen Afshin Jul 26 '17 at 06:20
  • What is the purpose of the lambda expression if you are hardcoding the parameter? Why not just say `2.ToString()`? – NetMage Jul 27 '17 at 18:10
  • @NetMage, look at the first example where the parameter is used in a complex expression and multiple places – Mohsen Afshin Jul 28 '17 at 04:30
  • And yet `$"{commandParameters[2].ParameterName} : {commandParameters[2].Value}"` still seems feasible, especially since this is for debugging. – NetMage Jul 28 '17 at 17:20
  • @NetMage it's just a simple example, one can have more complex queries to be evaluated during runtime, then how many times he/she should repeat and change the input values? – Mohsen Afshin Jul 29 '17 at 03:10

0 Answers0