Delegate, Lambda expression, callback

Source: Internet
Author: User

Today, I took over the WPF version of the Traffic Management project. As the first person took the lead, I was responsible for a bunch of semi-mature code, and then I started the painful process of reading the code.

My predecessors have been a master who has been engaged in five years. The code is very advanced, and various commissions, events, lambda, and Callbacks are everywhere. To me, I am a little white man who only understands simple delegation and events, reading can be painful. The most painful thing is that there were few or even no comments, and I was speechless. Work has to be done. After all, it has to be mixed up, so I will read the code he wrote. Today, let's take a look at a method in one of his codes as follows.

 

The following method is used to execute a query.

 1 /// <summary> 2         /// 执行查询 3         /// </summary> 4         /// <param name="startTime"></param> 5         /// <param name="endTime"></param> 6         /// <param name="listGateChannel"></param> 7         /// <param name="condition"></param> 8         /// <param name="page"></param> 9         /// <param name="pageSize"></param>10         private void QueryFromServer(DateTime startTime, DateTime endTime, List<RmpGateChannel> listGateChannel, string condition, int page, int pageSize)11         {12             m_rmpServiceHelper.GetVehicleAccess(startTime, endTime, listGateChannel, condition, pageSize, (lstVehicle) =>13             {14                 if (lstVehicle == null || lstVehicle.Count == 0)15                 {16                     MessageHelper.Show("未查询到相关记录", MessageType.Ok, 1);17                     return;18                 }19                 _listVehicle.Clear();20                 foreach (VehicleInfoDisplay item in lstVehicle)21                 {22                     _listVehicle.Add(item);23                     _allDataItems.Add(item);24                     dataPager.IsShowMoreButton = true;25                 }26                 dataPager.TotalCount = _allDataItems.Count;27             });28 29         }
View code

This method is the last parameter above (also a method in the anonymous method)

 1 /// <summary> 2         /// 获取第一页通行记录 3         /// </summary> 4         /// <param name="startTime"></param> 5         /// <param name="endTime"></param> 6         /// <param name="lstChannel"></param> 7         /// <param name="condition"></param> 8         /// <param name="callBack"></param> 9         public void GetVehicleAccess(DateTime startTime, DateTime endTime, List<RmpGateChannel> lstChannel, string condition, int pageSize, Action<List<VehicleInfoDisplay>> callBack)10         {11             CommunicationHelper.Instance.SendMessage((client) =>12             {13                 string[] arrTypes = new string[lstChannel.Count];14                 int?[] arrIds = new int?[lstChannel.Count];15                 for (int i = 0; i < arrTypes.Length; i++)16                 {17                     arrTypes[i] = "HostChannel";18                 }19                 for (int i = 0; i < lstChannel.Count; i++)20                 {21                     arrIds[i] = lstChannel[i].InternalObjectId;22                 }23                 m_pageInfo = client.PagePlusVehicleAccessInfoLogEx2(startTime, endTime, arrTypes, arrIds, condition, "", 1, pageSize);24                 List<vehicleAccessInfoLogEx3> tempList = new List<vehicleAccessInfoLogEx3>();25                 if (m_pageInfo.recordTotalNum > 0 && m_pageInfo.vehicleAccessInfo.Length > 0)26                 {27                     tempList.AddRange(m_pageInfo.vehicleAccessInfo.OrderBy(o => o.accessTime));28                 }29                 List<VehicleInfoDisplay> displayList = new List<VehicleInfoDisplay>();30                 foreach (vehicleAccessInfoLogEx1 obj in tempList)31                 {32                     VehicleInfoDisplay display = VehicleAccessInfoToDisplay.GetVehicleInfo(obj);33                     displayList.Add(display);34                 }35                 if (callBack != null)36                 {37                     DispatcherHelper.RunSync(new Action(() =>38                     {39                         callBack(displayList);40                     }), null);41                 }42             });43 44         }
View code

Is it a little big after reading it. After various data queries and analysis, we found that the last parameter of the first method is a Lambda expression, that is, an anonymous method. When a method is passed as a parameter, we first think of it as a delegate, and then execute some operations in this anonymous method. At this time, we find that this is still a callback. Why? We didn't pass the value to this anonymous method, but we did use this value parameter. According to the callback definition, it becomes clear that the called function passes a method to the main function, the main function passes a value to the called function, and then executes the called function.

 

Delegate, Lambda expression, callback

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.