PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 4, 2022

[FIXED] What exactly is the type of lambda expression with a body?

 November 04, 2022     .net, c#, lambda     No comments   

Issue

Windows forms extension method Invoke() doesn't accept a lambda expression, without us having to first typecast it to a delegate type like Action. This makes me wonder, if lambda expression (with a body) is not explicitly a delegate nor an expression, what is its type?


Solution

This makes me wonder, if lambda expression (with a body) is not explicitly a delegate nor an expression, what is its type?

It doesn't have a type in the normal sense of the word (i.e. a CLR type), just like null doesn't have a type. (Older versions of the C# specification had the concept of a "null type", but that was removed.)

Instead, it's an expression which is convertible to any compatible concrete delegate or expression tree type.

See section 7.1 of the C# 5 specification ("Expression Classification") for details - the relevant bullet points (out of the list of kinds of expression) are:

  • A null literal. An expression with this classification can be implicitly converted to a reference type or nullable type.
  • An anonymous function. An expression with this classification can be implicitly converted to a compatible delegate type or expression tree type.


Answered By - Jon Skeet
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing