C#/いまさらヒトに聞けないこといろいろ/汎用的なデリゲート
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
[[C#/いまさらヒトに聞けないこといろいろ/デリゲート]] で...
**ちょっと整理 [#r31afff8]
[[C#/いまさらヒトに聞けないこといろいろ/デリゲート]] の...
namespace ConsoleApplication1
{
delegate int Calculator(int x, int y);//delegateの宣言
class Program
{
static void Main(string[] args)
{
calc(2, 3, methodPlus);
calc(2, 3, methodMult);
}
public static void calc(int x, int y, Calculator ins...
{
int result = instance(x, y);
Console.WriteLine(result);
}
public static int methodMult(int x, int y)
{
return x * y;
}
public static int methodPlus(int x, int y)
{
return x + y;
}
}
}
** 汎用的なデリゲートFunc [#vac1968a]
上では
delegate int Calculator(int x, int y);
というデリゲートを宣言しましたが、引数がint,int,戻り値がi...
Func<int, int, int>
を使うことで、ソースは以下のように書き直すことができます
namespace ConsoleApplication1
{
//delegate int Calculator(int x, int y); //コメント...
class Program
{
static void Main(string[] args)
{
calc(2, 3, methodPlus);
calc(2, 3, methodMult);
}
public static void calc(int x, int y, Func<int, ...
{
int result = instance(x, y);
Console.WriteLine(result);
}
public static int methodMult(int x, int y)
{
return x * y;
}
public static int methodPlus(int x, int y)
{
return x + y;
}
}
}
ソースからデリゲートの宣言を消すことが出来ました。こう書...
**いろいろな書き方 [#a22e8f59]
これら、全部おんなじ意味です
calc(2, 3, methodPlus); // メソッドを渡す
calc(2, 3, new Func<int, int, int>(methodPlus)); // ne...
calc(2, 3, new Func<int, int, int>(delegate(int x, int y...
calc(2, 3, new Func<int, int, int>((int x, int y) => { r...
calc(2, 3, (int x, int y) => { return x + y; }); // ラ...
calc(2, 3, (x, y) => { return x + y; }); // ラムダ式は...
calc(2, 3, (int x, int y) => x + y);
**戻り値のない汎用デリゲート Action [#def8ee04]
つぎにActionデリゲートですが、こちらは戻り値のないFuncっ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
new Program().execute();
}
public void StringAction(string str)
{
Console.WriteLine("StringAction method start.");
Console.WriteLine(str);
Console.WriteLine("StringAction method end.");
}
public void execute()
{
// GenericsなdelegateをStringとして宣言
Action<String> StringActionDele = new Action<Strin...
//Action<String> StringActionDele = StringAction; ...
// delegate経由でメソッドを実行
StringActionDele("StringActionDeleを直接呼び出しま...
// メソッドの引数にdelegateインスタンスを渡すこと...
executeAction(StringActionDele);
// Anonymousな感じで直接定義してもいい
executeAction(
new Action<String>(delegate(String str)
{
Console.WriteLine("Anonymous Delegate method sta...
Console.WriteLine(str);
Console.WriteLine("Anonymous Delegate method end...
})
);
// 最後はラムダ式。Actionなくなった。引数ひとつでv...
// 渡すから、よろしくねって感じになる
executeAction((str) =>
{
Console.WriteLine("Delegate method start.");
Console.WriteLine(str);
Console.WriteLine("Delegate method end.");
});
List<string> stringList = new List<string>();
stringList.Add("こんにちは");
stringList.Add("さようなら");
stringList.ForEach(StringActionDele); // このメソ...
}
void executeAction(Action<String> actionDelegate)
{
actionDelegate("StringActionDeleをexecuteAction メ...
}
}
}
**関連リンク [#kb5fb20d]
-C#/いまさらヒトに聞けないこといろいろ/デリゲート
-[[C#と諸々 今更 Func デリゲートや Action デリゲートにつ...
-[[Action(T) デリゲート (System)>http://msdn.microsoft.co...
-[[Func(TResult) デリゲート (System)>http://msdn.microsof...
-[[ジェネリックなメソッドやデリゲートがもたらす新スタイル...
----
この記事は
#vote(おもしろかった,そうでもない)
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}
終了行:
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
[[C#/いまさらヒトに聞けないこといろいろ/デリゲート]] で...
**ちょっと整理 [#r31afff8]
[[C#/いまさらヒトに聞けないこといろいろ/デリゲート]] の...
namespace ConsoleApplication1
{
delegate int Calculator(int x, int y);//delegateの宣言
class Program
{
static void Main(string[] args)
{
calc(2, 3, methodPlus);
calc(2, 3, methodMult);
}
public static void calc(int x, int y, Calculator ins...
{
int result = instance(x, y);
Console.WriteLine(result);
}
public static int methodMult(int x, int y)
{
return x * y;
}
public static int methodPlus(int x, int y)
{
return x + y;
}
}
}
** 汎用的なデリゲートFunc [#vac1968a]
上では
delegate int Calculator(int x, int y);
というデリゲートを宣言しましたが、引数がint,int,戻り値がi...
Func<int, int, int>
を使うことで、ソースは以下のように書き直すことができます
namespace ConsoleApplication1
{
//delegate int Calculator(int x, int y); //コメント...
class Program
{
static void Main(string[] args)
{
calc(2, 3, methodPlus);
calc(2, 3, methodMult);
}
public static void calc(int x, int y, Func<int, ...
{
int result = instance(x, y);
Console.WriteLine(result);
}
public static int methodMult(int x, int y)
{
return x * y;
}
public static int methodPlus(int x, int y)
{
return x + y;
}
}
}
ソースからデリゲートの宣言を消すことが出来ました。こう書...
**いろいろな書き方 [#a22e8f59]
これら、全部おんなじ意味です
calc(2, 3, methodPlus); // メソッドを渡す
calc(2, 3, new Func<int, int, int>(methodPlus)); // ne...
calc(2, 3, new Func<int, int, int>(delegate(int x, int y...
calc(2, 3, new Func<int, int, int>((int x, int y) => { r...
calc(2, 3, (int x, int y) => { return x + y; }); // ラ...
calc(2, 3, (x, y) => { return x + y; }); // ラムダ式は...
calc(2, 3, (int x, int y) => x + y);
**戻り値のない汎用デリゲート Action [#def8ee04]
つぎにActionデリゲートですが、こちらは戻り値のないFuncっ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
new Program().execute();
}
public void StringAction(string str)
{
Console.WriteLine("StringAction method start.");
Console.WriteLine(str);
Console.WriteLine("StringAction method end.");
}
public void execute()
{
// GenericsなdelegateをStringとして宣言
Action<String> StringActionDele = new Action<Strin...
//Action<String> StringActionDele = StringAction; ...
// delegate経由でメソッドを実行
StringActionDele("StringActionDeleを直接呼び出しま...
// メソッドの引数にdelegateインスタンスを渡すこと...
executeAction(StringActionDele);
// Anonymousな感じで直接定義してもいい
executeAction(
new Action<String>(delegate(String str)
{
Console.WriteLine("Anonymous Delegate method sta...
Console.WriteLine(str);
Console.WriteLine("Anonymous Delegate method end...
})
);
// 最後はラムダ式。Actionなくなった。引数ひとつでv...
// 渡すから、よろしくねって感じになる
executeAction((str) =>
{
Console.WriteLine("Delegate method start.");
Console.WriteLine(str);
Console.WriteLine("Delegate method end.");
});
List<string> stringList = new List<string>();
stringList.Add("こんにちは");
stringList.Add("さようなら");
stringList.ForEach(StringActionDele); // このメソ...
}
void executeAction(Action<String> actionDelegate)
{
actionDelegate("StringActionDeleをexecuteAction メ...
}
}
}
**関連リンク [#kb5fb20d]
-C#/いまさらヒトに聞けないこといろいろ/デリゲート
-[[C#と諸々 今更 Func デリゲートや Action デリゲートにつ...
-[[Action(T) デリゲート (System)>http://msdn.microsoft.co...
-[[Func(TResult) デリゲート (System)>http://msdn.microsof...
-[[ジェネリックなメソッドやデリゲートがもたらす新スタイル...
----
この記事は
#vote(おもしろかった,そうでもない)
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}
ページ名: