From 2a1cd4fda8a4a8e649910d16b4dfa1ce7ae63543 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 12 May 2023 09:24:40 +0800 Subject: *misc --- .../prerequisites/using-and-dispose/index.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/using-and-dispose/index.md (limited to 'ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/using-and-dispose/index.md') 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) { } +``` -- cgit v1.1-26-g67d0