Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a List exist without <T> after it? [closed]

Tags:

c#

types

list

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;
        }
like image 720
Todo Avatar asked Mar 15 '13 09:03

Todo


3 Answers

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>;
    
like image 105
Marc Gravell Avatar answered Nov 10 '22 03:11

Marc Gravell


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.

like image 42
Konamiman Avatar answered Nov 10 '22 02:11

Konamiman


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>.

like image 1
Neil Barnwell Avatar answered Nov 10 '22 02:11

Neil Barnwell