LoginSignup
0
0

More than 1 year has passed since last update.

【Unity】ラムダ式とデリゲート型

Last updated at Posted at 2021-09-17

C++でお世話になっていたラムダ式を、Unityでも利用したいと思い調べました。

C++のラムダ式
auto Func = [](){};
Func();
C#のラムダ式
using System;
using System.Threading.Tasks;

Action Func = () => {};
Func();

C++ のようにいちいちデリゲート型を宣言せずに型推論で利用したいところですが、「var」にラムダ式で生成した関数を入れようとするとコンパイルエラーになるので、どうしても利用したい場合には補助メソッドを作る必要がありそうです。(追記:C# 10.0 ではすでに実装可能とのことです)

  • 主なデリゲート(関数を入れる変数)
    • Action
    • Action<引数の型>
    • Func<返り値の型>
    • Func<返り値の型, 引数の型>

ラムダ式やデリゲート型については下記サイト様が詳しく説明されています😊
 ➡ https://qiita.com/toRisouP/items/98cc4966d392b7f21c30

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0