I get a CA2000 error from my code analysis asking me to dispose my objects before exiting the scope
Dispose objects before losing scope In method
‘MyMethod(Guid?)’, call
System.IDisposable.Dispose on object ‘person’ before all references to
it are out of scope.
I’m surprised of this rule because I thought the dispose was automatically run when exiting the scope. Note : A similar question has already been asked and confirm my opinion to not force the dispose procedure.
What happens if I don’t dispose my object and i don’t use it in a using instruction?
Not any scope, only a
usingblock scope. Exiting a method is not the same as exiting ausingblock.If an object implements
IDisposable, then you should always dispose it manually or via ausingblock. It is up to you when to do this, based on the structure of your code.