Why calling Dispose() on BinaryReader results in compile error?

前端 未结 3 1466
时光取名叫无心
时光取名叫无心 2021-01-15 03:41

I have the following class which uses BinaryReader internally and implements IDisposable.

class DisposableClass : IDisposable
    {
        private BinaryReader r         


        
3条回答
  •  旧巷少年郎
    2021-01-15 04:25

    It won't work because the Dispose method on BinaryReader has been explicitly implemented.

    Instead of being implicitly implemented, as in:

    public void Dispose()
    {
    }
    

    ...it has been explicitly implemented, as in:

    void IDisposable.Dispose()
    {
    }
    

    ...which means it can only be accessed via the IDisposable interface. Therefore, you have to cast the instance to IDisposable first.

提交回复
热议问题