Z
Z
Zalim Lampezhev2015-02-24 13:29:35
Programming
Zalim Lampezhev, 2015-02-24 13:29:35

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)
  {
    // возврат списка стран (лист классов)
    // запрос к БД
  }
}

I can't find what I'm looking for on Google. I would be glad if you look for me, but it's better if you help me figure it out :)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
mayorovp, 2015-02-25
@mayorovp

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 { ... },
};

Alternatively, the list can be created empty and filled in by calling the Add method (the list constructor, by the way, does this "behind the scenes").
Array constructor:
new[] {
  new MainLand { ... },
  new MainLand { ... },
  new MainLand { ... },
  new MainLand { ... },
  new MainLand { ... },
};

As an alternative, you can create a list and call the ToArray() method on it.
The enum generator:
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 { ... };
}

You can also just create a list or an array - they are both enums.
---
Or do you have a problem accessing the database? But then you need to ask a question about the database, and not about the list.
Well, advice: find at least some textbook on C#! You are now asking a question that is described in any textbook ... Is it really easier to ask a question and wait for an answer for three days than to find the answer in a textbook in these three days?

S
Sergey, 2015-02-25
@senal

Here is an example

A
Anton Pronin, 2015-02-24
@nightw0rk

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 example
public int GetId(){get{ return id;} private set;}

Z
Zalim Lampezhev, 2015-02-24
@sabolch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question