Как работает return в методе: static int TestDelegate(CountDelegate method, string testString){return method(testString);} - вопрос №1444307

Обьясните пожалуйста конструкцию: return method(testString);
08.04.15
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; }
    }
}

08.04.15
Посмотреть всех экспертов из раздела Технологии > .Net/C#
Пользуйтесь нашим приложением Доступно на Google Play Загрузите в App Store