Last active
September 6, 2016 03:02
-
-
Save munron/622770090db17dedadd61c18d8a1bebc to your computer and use it in GitHub Desktop.
C#のデリゲートを理解する Func<T,returnT> Action<T>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
//デリゲートを理解するためのサンプルプログラム | |
namespace ConsoleApplication7 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//デリゲート | |
//宣言 : Func<引数1の型,引数2の型...,戻り値の型> 変数名 | |
//戻り値がない場合はAction<T>を使う | |
Func<int, int, int> func; | |
//呼び出す関数名funcは同じだが挙動が変わる | |
func = addNumber; | |
Console.WriteLine(func(5, 6)); | |
func = subNumber; | |
Console.WriteLine(func(5, 6)); | |
func = multiNumber; | |
Console.WriteLine(func(5, 6)); | |
Console.ReadKey(); | |
//デリゲートとはこのように関数の挙動を動的に変化させたいときに使う機能である。 | |
} | |
public static int addNumber(int a , int b) | |
{ | |
return a + b; | |
} | |
public static int subNumber(int a, int b) | |
{ | |
return a - b; | |
} | |
public static int multiNumber(int a, int b) | |
{ | |
return a * b; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment