using System;
using System.Collections;
namespace Chapter01All
{
delegate ArrayList PlusElementListUp(int[] x);
class DelegateLambdaSample2
{
static void Main()
{
int[] a = { 10, -5, 3, -9, 5 };
PlusElementListUp op = n =>
{
ArrayList data = new ArrayList();
for(int i=0; i<a.Length; i++)
{
if (a[i] > 0)
data.Add(a[i]);
}
return data;
};
ArrayList b = op(a);
foreach (int x in b)
Console.WriteLine(x);
}
}
}
delegateを使う場合でも、基本は使わない場合でも、書けることのようです。
using System;
using System.Collections;
namespace Chapter01All
{
class DelegateLambdaSample2
{
static void Main()
{
int[] a = { 10, -5, 3, -9, 5 };
ArrayList b = CheckArray(a);
foreach (int x in b)
Console.WriteLine(x);
}
static ArrayList CheckArray(int[] ary)
{
ArrayList data = new ArrayList();
int j = 0;
for (int i = 0; i < ary.Length; i++)
{
if (ary[i] > 0)
data.Add(ary[i]);
}
return data;
}
}
}
上の方を先にやって見て、delegateを使う方はそれを変更して見ました。
using System;
using System.Collections;
namespace Chapter01All
{
delegate ArrayList PlusElementListUp(int[] x);
class DelegateLambdaSample2
{
static void Main()
{
int[] a = { 10, -5, 3, -9, 5 };
PlusElementListUp Op = (int[] n) =>
{
ArrayList data = new ArrayList();
for(int i=0; i<a.Length; i++)
{
if (n[i] > 0)
data.Add(n[i]);
}
return data;
};
ArrayList b = Op(a);
foreach (int x in b)
Console.WriteLine(x);
}
}
}
PlusElementsListUpはint[] xの引数を持つので、PlusElementListUp Op = (int[] n)と書かないといけないかもしれませんが、一番上の様でも動くことは動いてるようでした。