- Home /
How to make an IList of a certain type?
I'm trying to make an IList of the type Vector2. Why doesn't this work..?
IList<Vector2> borderPixels;
Do I have to import something?
Are you trying to make a List(T)?
That's under the System.Collections.Generic namespace.
Answer by willparsons · Sep 09, 2015 at 08:18 AM
Make you class inherit from System.Collections.Generic and change the datatype from IList to just List.
List<Vector2> borderPixels = new List<Vector2>();
That should do the trick.
Answer by FortisVenaliter · Sep 08, 2015 at 07:52 PM
Because IList is an interface, not a class. Look up the interface keyword and abstraction/inheritance for more information, but you can never instantiate an abstract class or interface. You're probably looking for the List class instead.
Right.
So you can have your variable be declared as IList<Vector2>
but you need an actual class instance. So you can do this:
IList<Vector2> borderPixels = new List<Vector2>();
The List<T>
class implements the IList<T>
interface so you can assign a List<T>
instance to a IList<T>
variable. However you could use any other class that implements the IList<T>
interface. Using the IList interface usually only makes sense if you write some framework / abstraction yourself where your class doesn't need to know the actual implementation. It just needs "some kind" of "list".
In most cases you would use directly the type List<T>
List<Vector2> borderPixels = new List<Vector2>();
Your answer
Follow this Question
Related Questions
Find the highest first value of a vector2 in a list? 2 Answers
How to read X and Y component ( of a vector2) from a list 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers