8

I'm getting confused about all this talk about IDispose and "using" Statements. I wonder if someone can tell me if I need to use either a "using" Statement or some sort of implementation of IDispose in the following test example...

public class Main()
{
    MyFile myFile = new MyFile("c:\subdir\subdir2\testFile.txt");
    Console.Writeline("File Name: " + myFile.FileName() + "File Size: " + myFile.FileSize());
}

public class MyFile
{
    private FileInfo _fInfo;

    public MyFile(string fullFilePath)
    {
        _fInfo = new FileInfo(fullFilePath);
    }

    public string FileName()
    {
        return _fInfo.Name;
    }

    public long FileSize()
    {
        return _fInfo.Length;
    }

}
johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Ann Sanderson
  • 407
  • 3
  • 8
  • 17
  • First of all, in C#.NET, you can never be sure that you have disposed any object. GC does that for you. You can though tell few things in Destructor but still it won't deallocate memory at your will. Secondly, using statement is cleaner way than creating your code and implementing IDisposable, or creating Disposable methods. As the scope of using statement reaches end, all the variables defined and initialized inside are killed. –  Mar 19 '12 at 20:25
  • 9
    @AnubhavSaini: this is false. GC does not dispose for you, and you can always be sure you have disposed if you call Dispose. – John Saunders Mar 19 '12 at 20:29
  • GC collects. Disposing disposes. Calling Dispose method will sure that you disposed. When it gets disposed, no body knows. Still runtime destroyes objects. In most cases, use Using statement and try to write exception safe Disposing functions. @JohnSaunders thanks. –  Mar 19 '12 at 20:37
  • 6
    You're _still_ wrong. Objects get disposed _immediately_ when you call `Dispose`. You are confusing GC with disposal. – John Saunders Mar 19 '12 at 21:06
  • possible duplicate of [Proper use of the IDisposable interface](http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface) – John Saunders Mar 19 '12 at 21:06

4 Answers4

22

No, your example doesn't use any resources which need disposing (it doesn't touch anything which implements IDisposable, or have any direct handles to unmanaged resources) so you don't need to implement IDisposable.

Now if you changed your class to open the file, and maintain a FileStream field referring to the open file handle, then it would make sense to implement IDisposable to close the stream.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • While you're here, could you explain the part about the similarities between `using()` and the dispose methods? I remember another question where Lippert said something about there being very little difference, but I can't remember what exactly. – mowwwalker Mar 19 '12 at 21:00
  • 5
    @Walkerneo: A `using` statement is equivalent to a `try`/`finally` block, which disposes in the `finally`. – Jon Skeet Mar 19 '12 at 21:03
7

No, in the code provided I don't see any resource used that have to be disposed. So the answer is no, do not use in this code it.

Tigran
  • 61,654
  • 8
  • 86
  • 123
6

In your example there is nothing to dispose, so you have no need to do that.
If you want to read/write in file you can use Streamreader/Streamwriter for that and in that way you will need to dispose that objects after using them.

using(StreamReader sreader=new StreamReader())
{
//your code
}

So in this case using statement will help you to don't think about manually disposing/closing your object/

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
4

In your code there is no need for any Dispose / using - for a definitive guide on IDisposable see here.

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144