M
M
Maria2018-05-06 19:40:33
C++ / C#
Maria, 2018-05-06 19:40:33

How to create a field of type Generic List in a class?

Can you please tell me how to create a type field Generic List<T>in a class? Those. I need to be able to write a List of any type in the field when creating an instance of the class - for example, List<Album>or List<Photo>.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lab1
{
    public partial class AddForm : Form
    {
        public string itemToAdd { get; set; }
        public List<T> list { get; set; }

        public AddForm()
        {
            InitializeComponent();
        }

    }
}

I get the error The type or namespace name 'T' could not be found..

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
GavriKos, 2018-05-06
@GavriKos

https://docs.microsoft.com/en-us/dotnet/csharp/pro...

E
eRKa, 2018-05-06
@kttotto

If you want exactly " when creating an instance of a class , a List of any type could be written in the field", then the generic type must be defined at the compilation stage, and if you did not explicitly specify its type, then the compiler tries to find the class T in the current namespace, and its You don't, hence the error.
If you need to use a generic field inside a class, then you must define the class with the same type.

public partial class AddForm<T> : Form where T: class
{
    public string itemToAdd { get; set; }
    public List<T> list { get; set; }

    public AddForm()
    {
        InitializeComponent();
    }
}

And when creating an instance of the form, specify
In this case, the List field will be typed with the Album type.
If the list inside the form is small and you can neglect the cost of type casting, then you can make a list of type object.
public partial class AddForm : Form
{
    public string itemToAdd { get; set; }
    public List<object> list { get; set; }

    public AddForm()
    {
        InitializeComponent();
    }
}

And you can get from there by type in many ways, for example, if you know in advance what type it is, then make a ghost
If not, then filter by type
var albums = form.list.OfType<Album>();
var photos = form.list.OfType<Photo>();

C
Codebaker, 2018-05-06
@Codebaker

Maria , the List collection can only store objects of one type, so it seems that putting a couple of different types (Album and Photo) into it will no longer work. But this is only at first glance.
One of the pillars of OOP can come to the rescue: inheritance. If you make your Album and Photo types inherit from the same base type, such as MyObject, then you can create a list of common elements in the form: List.
This way you can add both Album and Photo to this list, but I'm sure that the next question will be how to get them from this list. Come to Toaster again. :-)

M
mefutu, 2018-05-06
@mefutu

In c# there is an ArrayList in which you can store objects of any type. You can check which object is possible with the is/as construct. Or you can just use List.
https://metanit.com/sharp/tutorial/4.3.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question