# V3074. The 'A' class contains 'Dispose' method. Consider making it implement 'IDisposable' interface.

- Scenario one
- Scenario two

The analyzer detected a method named 'Dispose' in a class that does not implement the 'IDisposable' interface. The code may behave in two different ways in the case of this error.

## Scenario one

The most common situation deals with mere non-compliance with the Microsoft coding conventions, which specify that method 'Dispose' is an implementation of the standard 'IDisposable' interface and is used for deterministic disposal of resources, including unmanaged resources.

Consider the following example:

    class Logger
    {
      ....
      public void Dispose()
      {
        ....
      }
    }

By convention, method 'Dispose' is used for resource freeing, and its presence implies that the class itself implements the 'IDisposable' interface. There are two ways to solve this issue.

1) Add an implementation of the 'IDisposable' interface to the class declaration:

    class Logger : IDisposable
    {
      ....
      public void Dispose()
      {
        ....
      }
    }

This solution allows using objects of class 'Logger' in the 'using' block, which guarantees to call the 'Dispose' method when leaving the block.

    using(Logger logger = new Logger()){
      ....
    }

2) Choose a neutral name for your method, for example 'Close':

    class Logger
    {
      ....
      public void Close()
      {
        ....
      }
    }

## Scenario two

The second scenario when this warning is triggered implies a potential threat of incorrect method call when the class is cast to the 'IDisposable' interface.

Consider the following example:

    class A : IDisposable
    {
      public void Dispose()
      {
        Console.WriteLine("Dispose A");
      }
    }
    class B : A
    {
      public new void Dispose()
      {
        Console.WriteLine("Dispose B");
      }
    }

If an object of class 'B' is cast to the 'IDisposable' interface or is used in the 'using' block, as, for example, in the following code:

    using(B b = new B()){
      ....
    }

then the 'Dispose' method will be called from class 'A'. That is, the 'B' class' resources won't be released.

To ensure that the method is correctly called from class 'B', we need to additionally implement the 'IDisposable' interface in it: then the 'Dispose' method will be called exactly from the 'B' class when its object is cast to the 'IDisposable' interface or used it in the 'using' block.

Fixed code:

    class B : A, IDisposable
    {
      public new void Dispose()
      {
        Console.WriteLine("Dispose B");
        base.Dispose();
      }
    }