In C# class Process
inherits from class Component
that implements IDisposable
and so I can call Dispose()
on any Process
object. Do I really have to? How do I know if I really have to?
Suppose I have the following code:
var allProcesses = System.Diagnostics.Process.GetProcesses();
var processesNames = processes.Select( p => p.ProcessName );
// output process names here
Now it looks like I have an array of Process
objects and I have craft a try-finally
to traverse the array and Dispose()
each object. That's definitely lots of extra code.
What does that Dispose()
do for Process
objects? Do I really need to Dispose()
every Process
object and how do I decide if I need to do so?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…