Sort a list of complex types using Comparision delegate in C shrap Arabic #78

 


Sort a list of complex types using Comparision delegate in C shrap Arabic #78

Suggested Videos

❑ Sorting a list of complex types using Comparison delegate.



Using Comparison delegate with List<T>

One of the overloads of the Sort() method in List class expects Comparison delegate to be passed as
an argument.

Approach1: Step1: Create a Function whose signature matches the signature of System.Comparsion delegate . This is the method where we need to write the logic to compare 2 customer objects.

Step 2: Create an instance of System.Comparsion delegate , and then pass the more of the function created in Step1 as the argument. So, at this point “Comparison ” delegate is pointing to our function that contains the logic to compare 2 customer objects.



Sorting a list of complex types

Approach2: In Approach 1 this is what we have done 1-Created a private function that contains the logic to compare customers. 2-Created an instance of comparison delegate , and then passed the name of the private function to the delegate. Finally passed the delegate instance to the Sort() method . Do we really have to follow all these steps. Isn’t there any other way? The above code can be simplified using delegate keyword as shown below.

Approach 3: The code in Approach 2 , can be further simplified using lambda expression as shown below.


Follow Us

YouTube

 If you want to download pptx : click Telegram

Facebook

Twitter

Instagram

Tik Tok


إرسال تعليق

أحدث أقدم