I came across this situation where List
has no type specified after it in <>
:
List someVar = new List();
However when I try this in Visual Studio I get an error. What is the reason for VS not letting me declare List
this way?
Let me show you where I saw it:
public override IEnumerable SomeMethod()
{
List someVar= new List();
// more code
return someVar;
}
P.S After contacting the owner of the project it turned out Wordpress striped out the tags <>
after List
and IEnumerable
, so it actually should be List<SomeClass>
and IEnumerable<SomeClass>
public override IEnumerable<SomeClass> SomeMethod()
{
List<SomeClass> someVar= new List<SomeClass>();
// more code
return someVar;
}
There is not an inbuilt class called List
. There is ArrayList
, but: click on List
and press f12. This will show you where it is declared. There are two options:
a class called List
that is nothing whatsoever to do with List<T>
has been declared in the local project; for example:
class List { ...} // here we go; a class called List
a using
alias (at the top of the file) has been used to spoof List
as a name; for example:
using List = System.Collections.Hashtable;
or
using List = System.Collections.Generic.List<int>;
You get an error because the List
class does not exist in the .NET Framework. If you want to use a non-generic list that can hold any type of object, use ArrayList.
I don't recognise it (I thought it was ArrayList
before List<T>
arrived?). Either way it would be an older class invented before generics was implemented. I'd use List<object>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With