Given the following method:
public static void DisposeItems<T>(this ICollection<T?> collection)
where T : class, IDisposable
{
foreach (var item in collection)
{
item?.Dispose();
}
if (!collection.IsReadOnly)
{
collection.Clear();
}
}
This is called in something like:
private static void CleanupNullable(List<TestClass?> collection)
{
collection.DisposeItems();
}
private static void Cleanup(List<TestClass> collection)
{
collection.DisposeItems();
}
The second gives the error: collection cannot be used to due difference of nullability in the reference types.
A similar implementation accepting IEnumerable<T?>
works fine because IEnumerable
is co-variant.
I cannot create an additional method accepting ICollection<T>
because nullability is not part of the signature. Is there any way I can compile this as it worked pre-nullability?
Dropping the class
constraint or changing it to class?
gives errors when calling it with Collection<TestClass?>
because TestClass?
doesn't match constraint IDisposable
.