In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the method as the parameter of the method in the C# delegation. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Regardless of the roundabout of the title or what a C# delegate is, let's take a look at the following two easiest ways to output a greeting on the screen:
Public void GreetPeople (string name) {/ / do some extra things, such as initialization, here EnglishGreeting (name);} public void EnglishGreeting (string name) {Console.WriteLine ("Morning," + name);}
Regardless of whether these two methods have any practical significance for the time being. GreetPeople is used to say hello to someone. When we pass a name parameter that represents someone's name, such as "Jimmy", in this method, the EnglishGreeting method will be called, passing the name parameter again, and EnglishGreeting will be used to output "Morning, Jimmy" to the screen.
Now suppose this program needs to be globalized. Oh, no, I am Chinese. I don't understand what "Morning" means. What should I do? All right, let's add a Chinese version of the greeting:
Public void ChineseGreeting (string name) {Console.WriteLine ("good morning," + name);}
At this time, GreetPeople also needs to be changed, otherwise how to determine which version of Greeting greeting method is appropriate? Before we do this, we define another enumeration as a basis for judgment:
Public enum Language {English, Chinese} public void GreetPeople (string name, Language lang) {/ / do some extra things, such as initialization, here swith (lang) {case Language.English: EnglishGreeting (name); break; case Language.Chinese: ChineseGreeting (name); break;}}
OK, although this solves the problem, I don't say it's easy for you to think that the scalability of this solution is very poor. If we need to add Korean and Japanese versions in the future, we will have to modify the enumeration and GreetPeople () methods repeatedly to meet the new requirements.
Before considering the new solution, let's take a look at GreetPeople's method signature:
Public void GreetPeople (string name, Language lang)
Let's just look at string name, where string is the parameter type and name is the parameter variable. When we assign the name string "jimmy", it represents the value of "jimmy"; when we assign it to "Zhang Ziyang", it represents the value of "Zhang Ziyang". We can then perform other operations on the name in the method body. Hey, is this nonsense? I knew it as soon as I learned the program.
If you think about it, if the GreetPeople () method can accept a parameter variable, that variable can represent another method, which represents the EnglsihGreeting () method when we assign the variable EnglishGreeting, and it represents the ChineseGreeting () method when we assign it ChineseGreeting. If we name this parameter variable MakeGreeting, isn't it possible to assign a value to the MakeGreeting parameter (ChineseGreeting or EnglsihGreeting, etc.) when calling the GreetPeople () method, just like when assigning a value to name? Then, we can also use MakeGreeting in the method body like any other parameter. However, because MakeGreeting represents a method, it should be used in the same way as the method it is assigned (such as ChineseGreeting), such as:
MakeGreeting (name)
Well, now that we have an idea, let's change the GreetPeople () method now, so it should look like this:
Public void GreetPeople (string name, * MakeGreeting) {MakeGreeting (name);}
Note that *, this position should usually be the type of parameter, but so far, we only think that there should be a parameter that can represent the method, and we rewrite the GreetPeople method in this way, and now a big question arises: what type of MakeGreeting parameter should this represent the method?
NOTE: enumerations are no longer needed here, because you dynamically decide which method to use when assigning a value to MakeGreeting, ChineseGreeting or EnglishGreeting, and within these two methods, a distinction has been made between "morning" and "good morning".
As smart as you should have thought, it's time for the delegate to come out, but before we talk about the delegate, let's take a look at the signatures of the ChineseGreeting () and EnglishGreeting () methods represented by the MakeGreeting parameter:
Public void EnglishGreeting (string name) public void ChineseGreeting (string name)
Just as name can accept "true" and "1" of type String, but cannot accept true of type bool and type 1 of int. The parameter type definition of MakeGreeting should be able to determine the types of methods that MakeGreeting can represent, and further, the parameter types and return types of methods that MakeGreeting can represent.
As a result, the delegate appears: it defines the type of method that the MakeGreeting parameter can represent, that is, the type of the MakeGreeting parameter.
NOTE: if the above sentence is a bit roundabout, I translate it like this: string defines the type of value that the name parameter can represent, that is, the type of the name parameter.
The definition of a C# delegate in this example:
Public delegate void GreetingDelegate (string name)
Can be compared with the signature of the EnglishGreeting () method above, except for the addition of the delegate keyword, are the rest exactly the same?
Now, let's change the GreetPeople () method again, as follows:
Public void GreetPeople (string name, GreetingDelegate MakeGreeting) {MakeGreeting (name);}
As you can see, delegate GreetingDelegate appears in the same place as string. String is a type, so GreetingDelegate should also be a type, or Class. But the way the delegate is declared is completely different from the class. What is this all about? In fact, delegates do compile to classes at compile time. Because Delegate is a class, delegates can be declared anywhere the class can be declared. More will be covered below, and now take a look at the complete code for this example:
Using System; using System.Collections.Generic; using System.Text; namespace Delegate {/ / defines a delegate, which defines the type of method public delegate void GreetingDelegate (string name) that can be represented; class Program {private static void EnglishGreeting (string name) {Console.WriteLine ("Morning," + name) } private static void ChineseGreeting (string name) {Console.WriteLine ("good morning," + name);} / / Note this method, which accepts a method of type GreetingDelegate as a parameter private static void GreetPeople (string name, GreetingDelegate MakeGreeting) {MakeGreeting (name) } static void Main (string [] args) {GreetPeople ("Jimmy Zhang", EnglishGreeting); GreetPeople ("Zhang Ziyang", ChineseGreeting); Console.ReadKey ();}
The output is as follows:
Morning, Jimmy Zhang good morning, Zhang Ziyang on "how to delegate the method as a method parameter" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.