Answer the question
In order to leave comments, you need to log in
How to return a list of classes List in C#?
You need to write a function that takes a string value and returns a list of classes. For example, it returns:
1 Russia
2 Ukraine
3 Germany
4 France
i.e. id and name. This needs to be passed as a class sheet. Here's what's there so far:
public class MLandl
{
public static class MainLand
{
public int id; // id материка
public int id_country; // id страны
public string name; // название страны
public string data_osn; // дата основания
public string data_ras; // дата распада
}
public static MainLand GetCountryList(string id)
{
// возврат списка стран (лист классов)
// запрос к БД
}
}
Answer the question
In order to leave comments, you need to log in
First, understand the terminology. Judging by your remarks, you need to return not a "list of classes", but a "list of instances of the MainLand class".
Secondly, what is the problem? The desired return type is List<MainLand>, IEnumerable<MainLand> or MainLand[] - choose any. Or do you have a problem in creating it? They are created like this:
List constructor:
new List<MainLand> {
new MainLand { ... },
new MainLand { ... },
new MainLand { ... },
new MainLand { ... },
new MainLand { ... },
};
new[] {
new MainLand { ... },
new MainLand { ... },
new MainLand { ... },
new MainLand { ... },
new MainLand { ... },
};
public static IEnumerable<MainLand> GetCountryList(string id)
{
yield return new MainLand { ... };
yield return new MainLand { ... };
yield return new MainLand { ... };
yield return new MainLand { ... };
yield return new MainLand { ... };
}
First, derive your MainLand class from IListSource,
Second, extract your class with the country as a separate class,
put the class in a List
your fields as id and name should be public and field;
For examplepublic int GetId(){get{ return id;} private set;}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question