passing generics in a function?

A

A. Burch

Is it legal to pass a generic object as a parameter, like the following
function is trying to do Queue<int>q. Please see the URL with the entire
example and can someone tell me why this doesn't compile and how to fix it:

private static void ShowQueueContents(Queue<int> q)
{
lock (((ICollection)q).SyncRoot)
{
foreach (int item in q)
{
Console.Write("{0} ", item);
}
}
Console.WriteLine();
}

The example below for a producer/consumer doesn't compile with VS 2005
correctly.
http://msdn2.microsoft.com/en-us/library/yy12yx1f.aspx
 
A

A. Burch

What is missing from the code for this error....
The error I got is related to the first argument in the function
Producer(Queue<int> q

public Producer(Queue<int> q, SyncEvents e)
{
_queue = q;
_syncEvents = e;
}

Error 1 The non-generic type 'System.Collections.Queue' cannot be used with
type arguments C:\MQueue\PCT\SyncEvents.cs
 
B

Barry Kelly

A. Burch said:
What is missing from the code for this error....
The error I got is related to the first argument in the function
Producer(Queue<int> q

public Producer(Queue<int> q, SyncEvents e)
{
_queue = q;
_syncEvents = e;
}

Error 1 The non-generic type 'System.Collections.Queue' cannot be used with
type arguments C:\MQueue\PCT\SyncEvents.cs

Add "using System.Collection.Generic;" to the top of your file.

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top