Целью приведенного ниже кода является определение определенного значения в общем списке с использованием метода List.Find(). Я вставляю код ниже:Поиск определенного значения внутри общего списка
class Program
{
public static List<Currency> FindItClass = new List<Currency>();
public class Currency
{
public string Country { get; set; }
public string Code { get; set; }
}
public static void PopulateListWithClass(string country, string code)
{
Currency currency = new Currency();
currency.Country = country;
currency.Code = code;
FindItClass.Add(currency);
}
static void Main(string[] args)
{
PopulateListWithClass("America (United States of America), Dollars", "USD");
PopulateListWithClass("Germany, Euro", "EUR");
PopulateListWithClass("Switzerland, Francs", "CHF");
PopulateListWithClass("India, Rupees", "INR");
PopulateListWithClass("United Kingdom, Pounds", "GBP");
PopulateListWithClass("Canada, Dollars", "CAD");
PopulateListWithClass("Pakistan, Rupees", "PKR");
PopulateListWithClass("Turkey, New Lira", "TRY");
PopulateListWithClass("Russia, Rubles", "RUB");
PopulateListWithClass("United Arab Emirates, Dirhams", "AED");
Console.Write("Enter an UPPDERCASE 3 character currency code and then enter: ");
string searchFor = Console.ReadLine();
Currency result = FindItClass.Find(delegate(Currency cur) { return cur.Code == searchFor; });
Console.WriteLine();
if (result != null)
{
Console.WriteLine(searchFor + " represents " + result.Country);
}
else
{
Console.WriteLine("The currency code you entered was not found.");
}
Console.ReadLine();
}
}
Мой запрос: почему List является статическим, в чем цель использования статического кода там.
public static List<Currency> FindItClass = new List<Currency>();
Другой вопрос: почему делегат используется внутри метода поиска.
Currency result = FindItClass.Find(delegate(Currency cur) { return cur.Code == searchFor; });
* Почему 'list' статична * Это должно быть вопрос указал автору этой программы?. Что касается 'делегата', это потому, что' Find' ожидает 'Predicate', где 'T' - тип вашего списка, поэтому вы можете предоставить [* анонимный метод *] (https://msdn.microsoft. com/en-us/library/0yw3tz5k.aspx). –
Google 'static', я уверен, что вы найдете ответ. Ключевое слово 'delegate' больше не требуется. Вы можете написать: 'FindItClass.Find (cur => cur.Code == searchFor);' –
Что вы думаете о том, почему делегат использовался для '.Find'? Считаете ли вы, что такое альтернативы? –