In .NET, is there a simple way for a class to be notified as it falls out of scope?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, with some languages. C++/CLI will emit Dipose calls for IDisposable implementers when their non-heap allocations drop out of scope (effectively giving them the same semantics as stack allocated resource in normal C++). Moreover, C++/CLI destructor syntax of ~Classname becomes an implementation of Dispose (and makes the class implement IDisposable).
I would expect other languages with traditional deterministic destruction to adopt this policy as time goes on. As others have mentioned, you can emulate it in C# with “using”, but it’s not quite the same.