in c# how can I extract the removed code below to be reuse for another classcardsOrderByDescendingc = codes removedToList;
To extract the removed code for reuse in another class, you can create a separate method or lambda expression that defines the sorting logic.
Here's an example of how you can achieve this:
- Create a new method or lambda expression that defines the sorting logic. Let's say you name it
SortByCode:
private int SortByCode(Card card)
{
// Add your sorting logic here
return card.Code;
}
- In your original code, replace the removed code with a reference to the
SortByCodemethod or lambda expression:
cards.OrderByDescending(SortByCode).ToList();
or
cards.OrderByDescending(card => SortByCode(card)).ToList();
- Now, you can reuse the
SortByCodemethod or lambda expression in another class by calling it directly with the appropriate card objects:
var sortedCards = cards.OrderByDescending(SortByCode).ToList();
or
var sortedCards = cards.OrderByDescending(card => SortByCode(card)).ToList();
By extracting the sorting logic into a separate method or lambda expression, you can reuse it in multiple places without duplicating the code
原文地址: https://www.cveoy.top/t/topic/h6ak 著作权归作者所有。请勿转载和采集!