Different compile-time behavior when implementing a generic class from Idisposable?

Why does the behavior change occur in case of the 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 left the implementation class empty, in the above case, it doesn't want me to implement the Dispose () method. However, below is the need to implement the Dispose () method. Below is the complete test code:

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()

      

+3


source to share


2 answers


If you have



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

      

Then I T

must realize IDisposable

.

If you have



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

      

Then I Repository

must implement IDisposable

.

If you want to instantiate test2<T>

, you must provide a generic parameter that is derived from testA

and implements IDisposable

.

+3


source


The first code example T

should implement IDisposable

. In the second code example, you Repository

have to implement it yourself IDisposable

.



+1


source







All Articles