Делегат не видит метод - C#
Формулировка задачи:
Делегат не видит метод который я ему передаю. ниже в коде я отметил место где происходит передача.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
delegate void strMod(ref string str);
namespace learning3
{
class Program
{
static void Main(string[] args)
{
strMod strOp;
strMod resplaceSp = new strMod(replaceSpaces ); <<<<< ТУТ НЕ МОГУ ПЕРЕДАТЬ МЕТОД. в чем причина ?
replaceSpaces подчеркивает красным в компиляторе
}
}
class StringOps
{
static void replaceSpaces (ref string a)
{
Console.WriteLine("Замена пробелов дефисами");
a = a.Replace(' ', '-');
}
static void removeSpaces(ref string a)
{
string temp = "";
int i;
Console.WriteLine("Удаление пробелов.");
for (i = 0; i < a.Length; i++)
if (a[i] != ' ') temp += a[i];
a = temp;
}
static void reverse(ref string a)
{
string temp = "";
int i = 0;
int j = 0;
Console.WriteLine("Вывод строки в обратном порядке");
for (j = 0, i = a.Length - 1; i >= 0; i--, j++)
temp += a[i];
a = temp;
}
}
}Решение задачи: «Делегат не видит метод»
textual
Листинг программы
using System;
namespace learning3
{
delegate void strMod(ref string str);
class Program
{
static void Main(string[] args)
{
string s = "Hello world;";
Foo(StringOps.removeSpaces,ref s);
Console.WriteLine(s);
s = "Hello wrold!";
Foo(StringOps.replaceSpaces, ref s);
Console.WriteLine(s);
Foo(StringOps.reverse, ref s);
Console.WriteLine(s);
}
static void Foo(strMod mod ,ref string s)
{
mod(ref s);
}
}
static class StringOps
{
public static void replaceSpaces(ref string a)
{
Console.WriteLine("Замена пробелов дефисами");
a = a.Replace(' ', '-');
}
public static void removeSpaces(ref string a)
{
string temp = "";
int i;
Console.WriteLine("Удаление пробелов.");
for (i = 0; i < a.Length; i++)
if (a[i] != ' ') temp += a[i];
a = temp;
}
public static void reverse(ref string a)
{
string temp = "";
int i = 0;
int j = 0;
Console.WriteLine("Вывод строки в обратном порядке");
for (j = 0, i = a.Length - 1; i >= 0; i--, j++)
temp += a[i];
a = temp;
}
}
}