Can you tell me is there any need for constructors in c# where properties are available to set default values?
Again, is there any need for destructors where the language is garbage collected?
Please give me some practical examples.
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.
Constructors are essential to initialize immutable data.
They also help with declaring IoC/DI expectations/demands.
For scenarios where there is a minimal set of required data to configure an object, it is useful to demand it as early as possible – which often means the constructor.
Destructors/finalisers are used generally to release unmanaged resources – for example OS handles, or memory from the unmanaged area (
Marshal.AllocHGlobal). These resources are not garbage collected, so care must be taken to release them manually – else a leak occurs, or you saturate limited pools. Such examples are pretty rare in application code, and are almost always used as a fallback in addition toIDisposable– for when it doesn’t get disposed correctly.