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
});
}
}
}
The first argument of
Parallel.ForEachshould be anIEnumerable<T>, aPartitioner<T>or something similar. YourUnOrderedBufferclass isn’t convertible to any of the parameter types involved. If you make it implementIEnumerable<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.