using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleDelegate1
{
class Program
{
//Объявление делегата с сигнатурой string(int)
delegate string ConvertsIntToString(int i);
static void Main(string[] args)
{
//someMethod - это переменная типа ConvertsIntToString
//От обычной ссылочной переменной она отличается тем, что
//указывает не на объект в куче, а на метод.
ConvertsIntToString someMethod = new ConvertsIntToString(HiThere);
Console.WriteLine(someMethod(1)+Environment.NewLine+"Для выхода нажмите любую клавишу!");
Console.ReadKey();
}
//Метод с сигнатурой string(int)
private static string HiThere(int i)
{
return "Hi there! #" + (i * 100);
}
}
}
************************Посмотреть*****************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.Move(10);
}
}
}
///////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
public class Student
{
public void Move(int distance)
{
for (int i = 1; i <= distance; i++)
{
Thread.Sleep(1000);
Console.WriteLine("Идет перемещение... Пройдено километров: {0}", i);
}
}
}
}
***************************Делегат****************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
public delegate void ShowMessage(string message);
public class Student
{
public void Move(int distance,ShowMessage method)
{
for (int i = 1; i <= distance; i++)
{
Thread.Sleep(1000);
method(string.Format("Идет перемещение... Пройдено километров: {0}", i));
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Student student = new Student();
ShowMessage method = Show;
student.Move(10,method);
}
static void Show(string message)
{
Console.WriteLine(message);
}
}
}
************************Обобщенный Делегат**************************
public class Student
{
public void Move(int distance,Action<string> method)
{
for (int i = 1; i <= distance; i++)
{
Thread.Sleep(1000);
method(string.Format("Идет перемещение... Пройдено километров: {0}", i));
}
}
}
///////////////////////////////////////////////////////////////////////////////////
class Program
{
static void Main(string[] args)
{
Student student = new Student();
Action<string> method = Show;
student.Move(10,method);
}
static void Show(string message)
{
Console.WriteLine(message);
}
}