question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Implicitly typed lambdas

See original GitHub issue

Currently, it is not possible to do this:

var x = () => 1;

Instead, you need to declare the type before:

Func<int> x = () => 1;

C# should just assume these kind of lambdas are either Action or Func and infer the type.

Usually these lambdas are used as helpers for the method implementation, so it really wouldn’t matter if a (string s) => true is a Func<string, bool> instead of a Predicate<string> as long as the signature matches.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:40 (19 by maintainers)

github_iconTop GitHub Comments

4reactions
DustinCampbellcommented, Jan 17, 2015

Not a perfect solution, but remember that you can create aliases for closed generic type names when they get long:

using MyFunc = System.Func<Board, int, int, System.Generic.Collections.IEnumerable<Board>>; 

The later:

MyFunc newBoard = (int rows, int cols, IEnumerable<Board> previousGames) => { 
   // calculate a game board
   return new Board(data);
};
2reactions
MadsTorgersencommented, Jan 16, 2015

This is one that we talk about often. VB has implicit types for delegates (not Func and Action but compiler generated ones), and quite liberal implicit conversions between delegate types.

We could do the same in C#, and maybe we should. One concern would be that the relative cost of these conversions might be on the highside, and invisible to users because of the implicit conversions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lambda improvements - C# 10.0 draft feature specifications
This feature specification describes a number of syntax improves that making declaring lambda expressions more convenient in a variety of ...
Read more >
Implicit conversion from lambda expression to user-defined ...
I want to define an implicit conversion from (specific) lambda expressions to a user-defined type. I tried the following:
Read more >
Lambda function versions
Lambda creates a new version of your function each time that you publish the function. The new version is a copy of the...
Read more >
Announcing AWS Lambda Function URLs: Built-in HTTPS ...
Today, I'm happy to announce the general availability of Lambda Function URLs, a new feature that lets you add HTTPS endpoints to any...
Read more >
Integrating Lambda Expressions and Events
Using lambda expressions is a more compact way of wiring up events in both C# and Visual Basic. It also provides a way...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found