0

.NET documentation states when the dispose pattern is supposed to be used.

It is when we interact with:

  1. file handles
  2. pipe handles
  3. registry handles
  4. wait handles
  5. pointers to blocks of unmanaged memory

What are some practical C# examples of each category above?

Patrik Mihalčin
  • 3,341
  • 7
  • 33
  • 68
  • Follow the stackoverflow question guidelines at https://stackoverflow.com/help/how-to-ask – SupaMaggie70 b Nov 30 '22 at 19:41
  • I would argue that file handle is pretty practical example. It is so much practical that usually you will not need to implement the dispose pattern for it yourself cause it is already implemented in the corresponding parts of the framework, exposing working with files via `FileStream` (directly or indirectly). – Guru Stron Nov 30 '22 at 19:44
  • 1
    In general, if you are dealing directly with unmanaged resources, then you class needs a _finalizer_ (aka destructor) and must follow the Dispose pattern to the letter. However as @GuruStron points out, you nearly never need to work at that level, instead using framework-provided (or other) abstractions like `FileStream`. Instances of those classes are disposable and you must Dispose them (generally using `using`). There's a ton of writeups about these topics floating around – Flydog57 Nov 30 '22 at 21:50

0 Answers0