diff options
Diffstat (limited to 'ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/using-and-dispose/index.md')
-rw-r--r-- | ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/using-and-dispose/index.md | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/using-and-dispose/index.md b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/using-and-dispose/index.md new file mode 100644 index 0000000..468d381 --- /dev/null +++ b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/using-and-dispose/index.md @@ -0,0 +1,21 @@ +# Using and Dispose + +Whenever you have an object the implements `IDisposable`, you need to dispose of the resource when you're done with it. Most classes that use unmanaged resources will implement `IDisposable`. This means a lot of classes in the `System.IO` namespace will need to be disposed of. + +The best practice to dispose of an object when you're done with it is to wrap the code in a `using` block. When the `using` block exits, the resource will automatically be disposed of as soon as possible. + +```cs +using (var stream = new MemoryStream()) +{ + // Use the stream. +} +// The stream will be disposed of as soon as possible. +``` + +If you need to keep keep it around for a while and dispose of it later, `using` does some error handling for you, so it's still a good idea to use it instead of calling `Dispose` directly. There is some debate on whether this is a good idea because it doesn't show intent. + +```cs +var stream = new MemoryStream(); +// Later in a different part of your code. +using (stream) { } +``` |