using System;
namespace Chapter01All
{
delegate string ProcName(string str);
class DelegateStaticMethod
{
static string Reverse(string str)
{
string ans = "";
for (int i = 0; i < str.Length; i++)
{
ans = str[i] + ans;
}
return ans;
}
static void Main()
{
ProcName instProc = Reverse;
string str = "This is a test.";
Console.WriteLine(instProc(str));
}
}
}
もう一度復習の意味で、やって見ます。
using System;
namespace Chapter01All
{
delegate string StrMod(string str);
class StringOps {
public string Reverse(string str)
{
string ans = "";
for (int i = 0; i < str.Length; i++)
{
ans = str[i] + ans;
}
return ans;
}
}
class DelegateTest
{
static void Main()
{
StringOps so = new StringOps();
string str;
StrMod strOp = so.Reverse;
str = strOp("This is a test.");
Console.WriteLine(str);
}
}
}
前回はReverseはstaticでしたが、今回はメンバ関数の場合です。staticからpublicに変わってます。注意しないと変わったのを見落としそうになります。