using System;
namespace Chapter01All
{
class TokumeiMethod
{
//デリゲートの宣言
delegate int CountIt(int limit);
delegate int CountThat(int limit);
delegate int CountThis(int limit);
static void Main()
{
//単なるstatic methodの起動
Console.WriteLine(Count(5));
//delegateを使ったCountメソッドの起動
CountIt Cnt = Count;
Console.WriteLine(Cnt(10));
//もう一度delegateを使ってCountメソッドの起動
CountThat count = Count;
Console.WriteLine(count(15));
//匿名メソッドを使うと
CountThis cnt = delegate (int l)
{
int wa = 0;
for (int i = 0; i <= l; i++)
{
Console.Write(i);
if (i != l)
Console.Write("+");
wa += i;
}
Console.Write(" = ");
return wa;
};
Console.WriteLine(cnt(20));
}
static int Count(int Limit)
{
int w = 0;
for (int i = 0; i <= Limit; i++)
{
Console.Write(i);
if (i != Limit)
Console.Write("+");
w += i;
}
Console.Write(" = ");
return w;
}
}
}
カテゴリー