Austen
Austen

Reputation: 293

Using the generic type 'System.Collections.Generic.List' requires '1' type argument(s)

I have an assignment to create a "Tweeter" app in c#, and for some reason I can't get it to do the most simple thing, add items to a list and display them.

I keep getting the error:

Using the generic type 'System.Collections.Generic.List' requires '1' type argument(s)

on the following line of code:

List tweets = new List<Tweet>();

I tried doing

Tweet tweets = new List<Tweet>();

first, but it gave me over a dozen errors.

Here's my Tweet.cs:

namespace Assign3_Twitter
{
    public class Tweet
    {
        public string HashTag { get; private set; }
        public string Message { get; private set; }
        public string Sender { get; private set; }
        public string Recipient { get; private set; }
        public Tweet (string sender, string message, string hashtag, string reciepient)
        {
            this.Sender = sender;
            this.HashTag = hashtag;
            this.Message = message;
            this.Recipient = reciepient;
        }

        public override string ToString ()
        {
            return string.Format ("[Tweet: HashTag={0}, Message={1}, Sender={2}, Recipient={3}]", HashTag, Message, Sender, Recipient);
        }
    }
}

and here's where I'm having all the issues:

TweetManager.cs

namespace Assign3_Twitter
{
    public class TweetManager
    {
        private List tweets = new List<Tweet>();

        public TweetManager()
        {
            tweets = new List<Tweet>();

            Tweet tw1 = new Tweet ("Austen", "Hello World!", "#Hey", "Twitter");
            tweets.Add (tw1);

            Tweet tw2 = new Tweet ("Test1", "Hello World! x2", "#Howdy", "Tweeter");
            tweets.Add (tw2);

            Tweet tw3 = new Tweet ("Test2", "Hello World! x3", "#Hey", "Twitter");
            tweets.Add (tw3);

            Tweet tw4 = new Tweet ("Test3", "Hello World! x4", "#Howdy", "Tweeter");
            tweets.Add (tw4);

            Tweet tw5 = new Tweet ("Test4", "Hey there!", "#Hey", "Twitter");
            tweets.Add (tw5);

            Tweet tw6 = new Tweet ("Test5", "Woah this is cool!", "#Howdy", "Tweeter");
            tweets.Add (tw6);
        }

        public void SeeAllTweeters()
        {
            foreach (Tweet Tweets in tweets)
            {
                Console.WriteLine (Tweets);
            }

        }

Upvotes: 2

Views: 9668

Answers (4)

Kyle W
Kyle W

Reputation: 3752

List tweets = new List<Tweet>();

should be

List<Tweet> tweets = new List<Tweet>();

In the first line you're trying to assign a typed list to an untyped list.

Also, as Kirk points out, simply using var instead of declaring the type yourself would automatically use the type that you intend:

var tweets = new List<Tweet>();

Upvotes: 13

TyCobb
TyCobb

Reputation: 9089

As others have pointed out.... you need to declare it as List<Tweet> or use var. var will automatically be turned into the correct type at compile time.

Now I am not sure if this is a simple mistake in forgetting to declare the generic or if you thought you just needed to declare List.

List doesn't actually exist. If you were looking to use something more generic, you could choose any of the interfaces on the List<T> that has the functionality you wanted. For instance, you could have used IList, IList<T>, ICollection, etc.

Setting your new List<Tweet>() to any of the interfaces above would have worked out as well.

http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564323

You need to either specify the full type (including the generics):

 List<Tweet> tweets = new List<Tweet>();

Or use implicit typing:

 var tweets = new List<Tweet>();

Upvotes: 0

Karthik Ganesan
Karthik Ganesan

Reputation: 4222

you have to do

List<Tweet> tweets = new List<Tweet>();

Upvotes: 0

Related Questions