Технологии
Как работает return в методе: static int TestDelegate(CountDelegate method, string testString){return method(testString);} - вопрос №1444307
Обьясните пожалуйста конструкцию: return method(testString);
апрель 8, 2015 г.
-
Всего ответов: 1
-
Здесь используются делегаты (см. msdn.microsoft.com/ru-ru/library/ms173171.aspx и msdn.microsoft.com/ru-ru/library/ms173172.aspx)
По факту, TestDelegate вернет на выходе результат вызова method(testString).
По всей видимости, CountDelegate объявлен что-то типа delegate int CountDelegate(string m_string);
Вот Вам пример программы:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
delegate int CountDelegate(string m_string);
static void Main(string[] args)
{
Console.WriteLine(TestDelegate(Add, «10»));
Console.WriteLine(TestDelegate(Sub, «10»));
Console.WriteLine(TestDelegate(Mul, «10»));
Console.WriteLine(TestDelegate(Div, «10»));
Console.ReadKey();
}
static int TestDelegate(CountDelegate method, string testString)
{
return method(testString);
}
static int Add(string tStr) { return 11; }
static int Sub(string tStr) { return 9; }
static int Mul(string tStr) { return 20; }
static int Div(string tStr) { return 5; }
}
}
Похожие вопросы
Решено
Проверка на существование записи перед добавлением в базу данных?
апрель 6, 2011 г.