In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to achieve entrustment in .net3.5. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
.net 3.5 introduces Linq,lambda expressions, so the creation of delegates becomes simpler and more elegant.
Delegate in .net 3.5
With the introduction of lambda expressions, anonymous methods are no longer needed, making it easier and more elegant to create delegates. In fact, if lambda expressions were introduced first, there would be no anonymous methods.
Lambda expressions are written in C # as "arg-list = > expr-body". The left side of the "= >" symbol is a list of parameters of the expression, and on the right is the expression body. The parameter list can contain 0 to more parameters, separated by commas.
1 namespace DelegateDemo 2 {3 / / statement delegation 4 public delegate void MyDel (string arg1, string arg2) 5 6 class Program 7 {8 static void Main (string [] args) 9 {10 / / .net 3.5 delegate 11 12 / / create delegate Use lambda expressions instead of anonymous methods 13 MyDel myDel = (string arg1, string arg2) = > 14 {15 Console.WriteLine (string.Format ("arg1: {0}, arg2: {1}", arg1, arg2)) 16}; 17 18 / call delegate 19 myDel ("aaa", "bbb"); 20 21 Console.ReadKey (); 22} 23} 24} omit parameter type
Because the compiler can know the type of delegate parameter from the delegate declaration (this feature is called type deduction), it allows us to omit the parameter type, so the code is simplified like this.
1 / / create delegate, using lambda expression instead of anonymous method 2 MyDel myDel = (arg1, arg2) = > 3 {4 Console.WriteLine (string.Format ("arg1: {0}, arg2: {1}", arg1, arg2)); 5}
Note that if there is only one parameter, you can also omit the parentheses around the parameter type.
Replace expressions with statements
Because the lambda expression allows the expression body to be a statement or a statement block, when the expression body has only one statement, the statement can replace the statement block and continue to simplify. The code is as follows:
1 / / create a delegate, using lambda expressions instead of anonymous methods 2 MyDel myDel = (arg1, arg2) = > Console.WriteLine (string.Format ("arg1: {0}, arg2: {1}", arg1, arg2)) This is the end of the article on "how to achieve delegation in .net3.5". 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 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.