This presentation is the property of its rightful owner.
Sponsored Links
1 / 20

Делегаты PowerPoint PPT Presentation


  • 74 Views
  • Uploaded on
  • Presentation posted in: General

Делегаты. Конструкторы Деструкторы Константы Поля Методы Свойства Индексаторы Операторы События Делегаты Классы Интерфейсы Структуры.

Download Presentation

Делегаты

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


6132447

  • , , C C++. . System.Delegate. , .

  • .

    • C++, .

    • .

    • .

    • ; ,


6132447

C -, .

. - .

Del, , string void:

publicdelegatevoid Del(string message);

delegate , .


6132447

, , .

publicstaticvoid DelegateMethod(string message)

{

System.Console.WriteLine(message);

}


6132447

#

, , . , , , ( ) .

. , , , , . :

Del handler = DelegateMethod; // Instantiate the delegate.

handler("Hello World"); // Call the delegate.


6132447

#

.NET : (static) (instance).

, . : .

, . .

, , . , .

, .


6132447

#

, , , .

, . , .

, .


6132447

C#

C#

C#

delegatevoid Del(); // Declare a delegate

class SampleClass

{

publicvoid InstanceMethod()

{ System.Console.WriteLine("A message from the instance method."); }

staticpublicvoid StaticMethod()

{ System.Console.WriteLine("A message from the static method."); }

}

class TestSampleClass

{

staticvoid Main()

{

SampleClass sc = new SampleClass();

Del d = sc.InstanceMethod;

d(); // Map the delegate to the instance method:

d = SampleClass.StaticMethod; // Map to the static method:

d();

}

}

/* Output:

A message from the instance method.

A message from the static method.

*/


6132447

C#

C#

, , , .

, Del, MultiplyNumbers


6132447

C#

C#

delegatevoid Del(int i, double j); // Declare a delegate

class MathClass

{

staticvoid Main()

{

MathClass m = new MathClass();

Del d = m.MultiplyNumbers; // Delegate instantiation using "MultiplyNumbers"

System.Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':");

for (int i = 1; i <= 5; i++) { d(i, 2); }

System.Console.WriteLine("Press any key to exit.");

System.Console.ReadKey();

}

void MultiplyNumbers(int m, double n) // Declare the associated method.

{

System.Console.Write(m * n + " ");

}

}

/* Output:

Invoking the delegate using 'MultiplyNumbers':

2 4 6 8 10

*/


6132447

#

. , , .

C#. C# 2.0, C# 3.0 - .

, , C# , .

. , . -.

-, .


6132447

#

, , . :

// Create a handler for a click event.

button1.Click += delegate(System.Object o, System.EventArgs e)

{ System.Windows.Forms.MessageBox.Show("Click!"); };

// Create a delegate.

delegatevoid Del(int x);

// Instantiate the delegate using an anonymous method.

Del d = delegate(int k) { /* ... */ };

, .


6132447

#

- , .

- , . - LINQ.

SQL XML-. , , C#, , SQL XQuery.

LINQ (Language-IntegratedQuery) C#. , , SQL, ADO.NET, XML- .NET Framework, IEnumerable, C# .


6132447

#

- ( ) - => .

, - x => x * x x x, . , .

lamda- , , .


6132447

C#

delegatedouble MathAction(double num); // delegate :

class DelegateTest

{

staticdouble Double(double input) //

{ return input * 2; }

staticvoid Main()

{

MathAction ma = Double; // delegate

double multByTwo = ma(4.5); // delegate ma:

Console.WriteLine("multByTwo: {0}", multByTwo);

MathAction ma2 = delegate(double input) // delegate :

{ return input * input; };

double square = ma2(5);

Console.WriteLine("square: {0}", square);

MathAction ma3 = s => s * s * s; // delegate lambda

double cube = ma3(4.375);

Console.WriteLine("cube: {0}", cube);

}

// Output:

// multByTwo: 9

// square: 25

// cube: 83.740234375

}


6132447

C#

-

-, , -.

- :

(input parameters) => expression

, , . , :

(x, y) => x == y

. , :

(int x, string s) => s.Length > x


6132447

C#

.

() => SomeMethod()

: - .


6132447

C#

( ) -, , ( ) .

(input parameters) => {statement;}

; -.

delegatevoid TestDelegate(string s);

TestDelegate myDel = n => { string s = n + " " + "World"; Console.WriteLine(s); };

myDel("Hello");


6132447

C#

- , , -.

, , - , .

, -.


6132447

C#

delegatebool D();

delegatebool D2(int i);

class Test

{

D del;

D2 del2;

publicvoid TestMethod(int input)

{

int j = 0;

del = () => { j = 10; return j > input; };

del2 = (x) => {return x == j; };

Console.WriteLine("j = {0}", j);

bool boolResult = del(); // Invoke the delegate.

Console.WriteLine("j = {0}. b = {1}", j, boolResult); }

staticvoid Main()

{

Test test = new Test();

test.TestMethod(5);

bool result = test.del2(10);

Console.WriteLine(result); // Output: True

Console.ReadKey();

}

}


6132447

C#

.


  • Login