If you need to access properties or methods from your generic type parameter, the simplest option is to have your types implement an interface, and constrain the type parameter to types which implement that interface.
For example:
public interface IObjectWithId
{
Guid ObjectId { get; }
}
public class TestClass : IObjectWithId
{
public Guid ObjectId { get; } = Guid.NewGuid();
}
public class ObjectPooler<T> where T : IObjectWithId
{
...
public T GetPoolObject(Guid objectId)
{
return _deepEndObjects.Find(x => x.ObjectId == objectId);
}
public bool RemovePoolObject(Guid objectId)
{
int numberOfObjectsRemoved = _deepEndObjects.RemoveAll(x => x.ObjectId == objectId);
return numberOfObjectsRemoved != 0;
}
}
NB: Object pools are frequently called from multiple threads at the same time. Your code is currently not thread-safe. If you call it from multiple threads, you will end up corrupting the state of the pool.
If that's a problem, you might want to look at using a
concurrent collection[
^]. For example:
How to: Create an Object Pool by Using a ConcurrentBag | Microsoft Docs[
^]