Nomi Ali
Nomi Ali

Reputation: 2252

Different behaviour at compile time while implementing generic class from Idisposable?

Why there is a change in behaviour in case of following codes

public class Repository<T> : IRepository<T> where T : BaseEntity, IDisposable

And

public class Repository<T> : IDisposable, IRepository<T> where T : BaseEntity

If I leave the implementation class empty, In above case it doesn't want me to implement Dispose() method. However, In below there is a need to implement Dispose() method. Below is complete test code as:

public interface Itest<T> where T: testA{ }
public class testA { }
public class test2<T> : Itest<T> where T : testA,IDisposable{ } //successfully compiled
public class test3<T> : IDisposable, Itest<T> where T : testA { }//Failed compiled : need to implement Dispose()

Upvotes: 3

Views: 88

Answers (2)

Hamid Pourjam
Hamid Pourjam

Reputation: 20754

When you have

public class Repository<T> : IRepository<T> where T : BaseEntity, IDisposable

Then T must implement IDisposable.

When you have

public class Repository<T> : IDisposable, IRepository<T> where T : BaseEntity

Then Repository must implement IDisposable.

When you want to create an instance of test2<T> you should provide a generic parameter which is derived from testA and implements IDisposable.

Upvotes: 3

Maarten
Maarten

Reputation: 22945

In the first code sample, the T must implement IDisposable. In the second code sample, the Repository itself must implement IDisposable.

Upvotes: 1

Related Questions