Canacourse
Canacourse

Reputation: 1865

The type arguments for method 'x' cannot be inferred from the usage using Parallel.ForEach

I am trying out the "where T :" construct for the first time and am getting an error when trying to use Parallel.ForEach.

"The type arguments for method 'System.Threading.Tasks.Parallel.ForEach(System.Collections.Concurrent.OrderablePartitioner, System.Action)' cannot be inferred from the usage."

I understand why the error is occuring but not how to fix it. Data is a simple class with only 2 properties.

namespace Test
{
  internal class UnOrderedBuffer<T> where T : class
    {

        ConcurrentBag<T> GenericBag = new ConcurrentBag<T>();
    }
}


namespace Test
{
    internal class Tester
    {

     private UnOrderedBuffer<Data> TestBuffer;


     public void Update()
    {

    Parallel.ForEach(TestBuffer, Item =>
            {
                //do stuff
            });
    }

    }
}

Upvotes: 2

Views: 2661

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502066

The first argument of Parallel.ForEach should be an IEnumerable<T>, a Partitioner<T> or something similar. Your UnOrderedBuffer class isn't convertible to any of the parameter types involved. If you make it implement IEnumerable<T> or something similar, then it'll work.

This isn't really about the type arguments or the generic constraints - it's about your class not implementing the right interfaces or extending an appropriate class.

Upvotes: 4

Related Questions