Return the real IEnumerable interface<t> by method, C#</t>



  • Good afternoon, I'm very much swimming in the topic of common interfaces, methods. It would be very grateful for the clue or the reference in which direction it should be replicated. Theory read from the professorweb.ru on the interface, synthesis. Theory of the theory that I understood what was not, but in practice, I still cannot fulfil the following task.

    For example, there is a code:

    DataPerson dataPerson;
    IEnumerable<Person> person = dataPerson.<IEnumerable<Person>>Get(); 
    // как я понял, метод Get() должен в person записать значение ссылочного типа 
    // на реализующий интерфейс типа Person? Не представляю как реализовать это.
    // В плане непонятны мне следующие вопросы: 
    

    // 1. Где следует реализовать этот интерфейс?

    // 2. Не понимаю про двойное обобщение вида <T<V>>,
    // а именно каким образом это указать в методе Get()?

    // 3. Не понимаю как вернуть интерфейс через метод.

    Class Person:

    class Person
    {
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Person(string fName, string lName)
    {
        FirstName = fName;
        LastName = lName;
    }
    

    }

    Class DataPerson:

    class DataPerson
    {
    // как я примерно представляю объявление функции Get()
    public IEnumerable Get<T,V>()
    where T : IEnumerable<V>
    {
    // как реализовать, к сожалению не представляю
    }
    }

    How do you realise the method Get() in DataPerson class?



  • Your method is likely to be announced as

    class DataPerson
    {
        public IEnumerable<T> Get<T>()
            where T : new()
        {
    
    }
    

    }

    I mean, he's actually only got one type parameter, the type of objects chosen.

    and used as

    IEnumerable<Person> person = dataPerson<Person>Get();

    It can be implemented in two ways:

    It is true to create something that operates the IEnumerable interface. Like a set or a list. Fill it and return it from the method:

    class DataPerson
    {
    public IEnumerable<T> Get<T>()
    where T : new()
    {
    var query = buildSomeQueryFor(typeof(T)); // построить запрос для выбора объектов типа T
    DataRow[] dataRows = selectSomeRowsFromDB(query); //

        var result = new List&lt;T&gt;();
    
        foreach (var row in dataRows)
        {
            result.Add(MapDataRowTo&lt;T&gt;(row));
        }
    
        return result;
    }
    
    private MapDataRowTo&lt;T&gt;(DataRow row)
        where T : new ()
    {
        var newObject = new T();
        // map values from row to newObject
    
        // return
        return newObject;
    }
    

    }

    The second way is to use the key word. yield:

    class DataPerson
    {
    public IEnumerable<T> Get<T>()
    where T : new()
    {
    var query = buildSomeQueryFor(typeof(T)); // построить запрос для выбора объектов типа T
    DataRow[] dataRows = selectSomeRowsFromDB(query); //

        foreach (var row in dataRows)
        {
            yield return MapDataRowTo&lt;T&gt;(row);
        }
    }
    
    private MapDataRowTo&lt;T&gt;(DataRow row)
        where T : new ()
    {
        var newObject = new T();
        // map values from row to newObject
    
        // return
        return newObject;
    }
    

    }

    Using yield will turn your meths into the vent-IEnumerable that will return the elements as they are contacted from the calling code. i.e. implementation Get<T> It's gonna be a bite, one challenge, and then it's the next. MSDN has sufficient detail https://msdn.microsoft.com/ru-ru/library/9k7k7cf0.aspx ♪




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2