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


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
23 views
Please tell me how to create a field of type Generic Listin class? Ie I need when creating a class instance in a field, it was possible to record List of any type, e.g. Listor List.

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 list { get; set; } public AddForm() { InitializeComponent(); } } }


Get the error The type or namespace name 'T' could not be found..
by | 23 views

4 Answers

0 like 0 dislike
by
0 like 0 dislike
If You want it "when you instantiate a class in the field it was possible to record List of any type", generic type must be defined at compile time and if You do not explicitly specified its type, the compiler tries to find the class T in the current namespace, and You don't have one from here and an error.

If you want within the class to use a generic field, then the class You need to define the same type.
public partial class AddForm : Form where T: class { public string itemToAdd { get; set; } public List list { get; set; } public AddForm() { InitializeComponent(); } }

And when you create instance of the form to specify
var form = new AddForm()
In this case, the List field will be typisierung type Album.

If the list inside the form of a small and possible to neglect the cost of the casting, it is possible to make a list of type object.
public partial class AddForm : Form { public string itemToAdd { get; set; } public List
list { get; set; } public AddForm() { InitializeComponent(); } }
But to get from there type can be in many ways, for example, if you know in advance what type to make a Ghost
var albums = form.list.Cast();
If not - then to do the filtering by type
var albums = form.list.OfType(); var photos = form.list.OfType();
by
0 like 0 dislike
Maria, the List can store objects of only one type, so it seems what to put in it a couple of different types (Album and Photo) will not succeed. But this is only at first glance.

The can help one of the pillars of OOP: inheritance. If you make your own Photo Album and heirs of the same base type, such as MyObject, then you will be able to create in the form of a list of shared items List.

This way you will be able to put in this list and Album and Photo, but I'm sure the next question will be - and how to get them from this list. Come to the Toaster again. :-)
by
0 like 0 dislike
C# has an ArrayList in it, you can store objects of any type. To check which object you design is/as. Or you can just use a List.

\rhttps://metanit.com/sharp/tutorial/4.3.php
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
3 answers
asked Apr 14, 2019 by adressmoeistranici
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
asked Jun 9, 2019 by itgood
110,608 questions
257,187 answers
0 comments
40,796 users