namespaceEffectiveCSharp.Item21 { public class EnginDriverTwo<T> : IDisposable where T : IEngine, new() { private Lazy<T> driver = new(()=> new T()); publicvoidGetThingDone() => driver.Value.DoWork(); publicvoidDispose() { if (driver.IsValueCreated) { var resource = driver.Value as IDisposable; resource?.Dispose(); } } } }
타입 매개변수로 맴버 변수를 선언하면 IDisposable을 상속 받아서 Dispose를 구현해주면 된다.
sealed 를 활용한 제한
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
namespaceEffectiveCSharp.Item21 { publicsealedclassEnginDriver<T> whereT : IEngine { private T driver;