rksoftware

Visual Studio とか C# とかが好きです

ラムダの機能強化 ラムダ属性

■ C# 10.0 での新機能

・ラムダの機能強化 ラムダ属性 (Lambda improvements)
  https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/lambda-improvements
 ラムダで属性が使える、また return 値の型も書ける。メソッドの引数などでラムダを渡したいときの扱いの改善。

MapAction([HttpPost("/")] ([FromBody] string todo) => todo);    // ラムダで属性が使える
MapAction(string (todo) => todo);   // 直接各ラムダで return の型ががける

//MapAction([HttpPost("/")] todo => todo);    // これはエラー。ラムダに属性をつける場合、引数リストは ( ) が必要
MapAction([HttpPost("/")] (todo) => todo);  // これは ( ) があるので OK

// 関数やクラスの定義

// Function
void MapAction(Func<string, string> action) {; }

// 属性
class HttpPostAttribute : Attribute { public HttpPostAttribute(string root) {; } }
class FromBodyAttribute : Attribute { public FromBodyAttribute() {; } }