Roy Shmuli
Roy Shmuli

Reputation: 5019

Why i get compilation error in C# with generic

I have method that contain parameter BlockingCollection<T> queue, and T must extend my class QueueItem (it's work fine without the generic).

private void ProcessQueue<T>(BlockingCollection<T> queue) where T: QueueItem
{
     QueueItem frame;
     while (true)
     {
          if (queue.TryTake(out frame, -1))
          {
              frame.execute();
          }
     }
}

The compilation error on if (queue.TryTake(out frame, -1)) : "the method has some invalid arguments

Why?

Edit the method definition is:

BlockingCollection<T> TryTake(T, Int32)

Upvotes: 0

Views: 49

Answers (1)

Lee
Lee

Reputation: 144206

frame should be a T but you are providing a QueueItem. Change the type of frame:

 T frame;
 while (true)
 {
      if (queue.TryTake(out frame, -1))
      {
          frame.execute();
      }
 }

Upvotes: 4

Related Questions