Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is. Net delegation

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is. Net entrustment". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is. Net entrustment"!

Recently, the work at hand is relatively fast, and I have time to study every day. I have been watching the commission and making a summary these days. If I am blurred in the future, I can remember it and do not understand the situation of using the commission.

Delegates can be customized, or you can use built-in delegates

Custom delegate: delegate T4 AllDeleate (T1a, T2b, T3c); / / the delegate written by you based on viewing the source code

No return value delegate:

Action action = (a) = >

{

For (int I = 0; I

< 10; i++) { Console.WriteLine(a + i); } }; 有返回值委托:Func func = (a) =>

A + "Hello"

Only one parameter returns a Boolean delegate:

Predicate pre = (d) = >

{

Int tag = 10

Bool bo = d > tag? True: false

Return bo

}

/ / write the delegation according to the source code

Var dele = new AllDeleate (TestDeleage)

Var str = dele ("I am a", "I am b", "I am c")

Console.WriteLine (str)

Public string TestDeleage (string a, string b, string c)

{

Return "writes the delegation according to the source code, and outputs a:" + a + ", b:" + b + ", c:" + c "

}

/ / built-in delegation and traversing the delegate output

Func func = GetName;// declares delegation and registers

Func + = (d) = > d + "I am an anonymous delegate"; / / register

Var deleteArray = func.GetInvocationList (); / / Multicast delegate call list

Foreach (Func item in deleteArray)

{

Var mag = item ("Xiao Li")

Console.WriteLine (mag)

}

Private string GetName (string name)

{

Console.WriteLine ("delegate calling method")

Return name + "Hello, welcome to delegate"

}

/ / predicate delegation

Predicate pre = (d) = >

{

Int tag = 10

Bool bo = d > tag? True: false

Return bo

}

Console.WriteLine ("predicate delegate anonymous function:" + pre (10))

Predicate preTest = GetBool

Console.WriteLine ("predicate delegate function:" + GetBool (12))

Private static bool GetBool (int num)

{

Int tag = 10

Bool bo = num > tag? True: false

Return bo

}

/ / write delegation

Var nums = TsetReturn ("Xiaoming", GetName)

Console.WriteLine (nums)

Public static T2 TsetReturn (T1 t, Func func)

{

Console.WriteLine ("entry method")

T2 num = func (t)

Return num

}

Private static string GetName (string name)

{

Console.WriteLine ("delegate calling method")

Return name + "Hello, welcome to delegate"

}

/ / entrust practical application

Public static void DiaoYongTest ()

{

DelteateClass delteateClass = new DelteateClass ()

DelteateClass.td = new TestDeleate (delegate (int I, int maxValue)

{

Console.WriteLine ("current Progress:" + I)

Console.WriteLine ("Max:" + maxValue)

});

DelteateClass.td + = new TestDeleate ((a, b) = >

{

Console.WriteLine ("Lambda current Progress:" + a)

Console.WriteLine ("Lambda Max:" + b)

});

DelteateClass.td + = new TestDeleate (GetProgress)

DelteateClass.ApplyTest ()

}

Private static void GetProgress (int I, int maxValue)

{

Console.WriteLine ("GetProgress current Progress:" + I)

Console.WriteLine ("GetProgress Max:" + maxValue)

}

Class DelteateClass

{

Public TestDeleate td

Public void ApplyTest ()

{

Int maxValue = 10

Var list = td.GetInvocationList ()

Foreach (var item in list)

{

For (int I = 0; I < maxValue; iTunes +)

{

Item?.DynamicInvoke (I, maxValue)

}

}

}

}

At this point, I believe you have a deeper understanding of "what is. Net entrustment". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report