Is there any need for constructors or destructors in C #?

Can you tell me if there is a need for constructors in C # where properties for setting default values ​​are available?

Again, is there a need for destructors where the language is garbage collected?

Please give me some practical examples.

+4
source share
5 answers

Constructors are needed to initialize immutable data. They also help with the announcement of IoC / DI expectations / requirements. For scenarios where there is a minimal set of required data for setting up an object, it is useful to require it as early as possible, which often means a constructor.

Destructors / finalists are typically used to free unmanaged resources - for example, OS descriptors or memory from an unmanaged area ( Marshal.AllocHGlobal ). These resources are not garbage collection, so care should be taken to free them manually - otherwise there will be a leak or you will run out of limited pools. Such examples are quite rare in the application code and are almost always used as a backup in addition to IDisposable - so that it is not installed correctly.

+5
source

Can you tell me if there is a need for constructors in C # where properties for setting default values ​​are available?

There are times when you explicitly indicate that a dependency class is required to work. In this case, you can use the constructor installation:

 public class MyClass { private readonly ISomeDependency _dependency; public MyClass(ISomeDependency dependency) { _dependency = dependency; } ... some methods that require the dependency. } 

Now the author of the class explicitly states that this class needs some dependency in order to work properly, so to create it you need to provide this dependency.

Again, is there a need for destructors where the language is garbage collected?

A language is garbage collection if it is code driven. But in managed code, you can use P / Invoke to invoke unmanaged code. And unmanaged code, obviously, no longer collects garbage. Thus, you can use destructors to free resources due to unmanaged code (things like unmanaged handles, unmanaged memory allocations, ...).

+4
source

One example where a constructor is absolutely necessary is immutable types. How else will your fields get their value?

Finalizers (Spec calls them destructors, but this is IMO stupidity) are usually needed only when working with unmanaged resources. But in most cases, a critical finalizer (using the SafeHandle family of classes) is the right choice for them.

The Dispose() method is useful when working with unmanaged resources, but also if you need to do something to destroy, for example, unsubscribing from events. It is also used along with using to create a lower version of RAII.

+4
source

You can use the constructor where every time an object is created, and if we want some code to be executed automatically. The code that we want to execute must be placed in the constructor. The general view of the C # constructor is as follows

 modifier constructor_name (parameters) { //constructor body } 

Modifiers can be private, public, secure or internal. The constructor name must be the name of the class where it is defined. The constructor may take zero or more arguments. A constructor with zero arguments (this is not an argument) is known as the default constructor. Remember that there is no return type for the constructor.

The following class contains a constructor that takes two arguments.

 class Complex { private int x; private int y; public Complex (int i, int j) { x = i; y = j; } public void ShowXY () { Console.WriteLine(x + "i+" + y); } } 

The next code segment displays 20 + i25 on the command line.

 Complex c1 = new Complex (20,25); c1.ShowXY (); // Displays 20+i25 

That is, when we create an object of class Complex, it automatically calls the constructor and initializes its data elements x and y. We can say that the constructor is mainly used to initialize the object. Even inside the constructor, you can make very complex calculations. A statement inside the constructor may also throw exceptions.

Destructors

The .NET framework has a built-in mechanism called Garbage Collection to de-allocate memory occupied by unused objects. The destructor implements the statements that must be executed during the garbage collection process. A destructor is a function with the same name as the class name, but starting with the ~ character.

Example:

 class Complex { public Complex() { // constructor } ~Complex() { // Destructor } } 

Remember that a destructor cannot have any modifiers such as private, public, etc. If we declare a destructor with a modifier, the compiler will show an error. Also, the destructor will be presented in only one form without any arguments. There is no parameterized destructor in C #.

Destructors are called automatically and cannot be called explicitly. An object becomes suitable for garbage collection when it is no longer used by the active part of the program. The execution of the destructor can occur at any time after the instance or object becomes suitable for destruction.

+1
source

In addition to what Mark has already mentioned, it is very rarely necessary to create a finalizer since the implementation of SafeHandle in .NET 2.0. For more details see http://blogs.msdn.com/b/bclteam/archive/2005/03/16/396900.aspx and http://www.bluebytesoftware.com/blog/2005/12/27/NeverWriteAFinalizerAgainWellAlmostNever.aspx .

0
source

All Articles