What it means '~' in front of the constructor of a C#class

Asked

Viewed 1,436 times

32

Working on a project here at the company, I came across some commands that are now being used more (yield, ??, anonymous type) but until I understood, but one who called my attention by not knowing what is.

Has anyone seen the following example?

public Perfil {
    public ICollection<Usuario> Usuarios { get; set; }
    ...

    ~Perfil() {
        Usuarios = new HashSet<Usuarios>();
    }
}

Who has seen and knows about it, could pass a brief explanation? It is just curiosity to understand better.

There are other similar?

  • 6

    A little outside the main scope of the question, but it may be of interest to you to know, the operator yield shall be understood as an operator of accumulation. For example: public IEnumerable<int> ListaDeNumeros() { for (int i = 1; i <= 100; i++) { yield return i; } } The code will make 100 Returns, accumulating the result in a kind of a List<int> with numbers from 1 to 100.

  • @Ciganomorrisonmendez Yes, Yield I met a little while ago and I find very interesting the use of it, as I said is that really since the first time I started programming 4 years ago, I believe today must have been the first time I saw the destructor in C# rsrs..

2 answers

26


This is not a builder, but a destroyer.

The destructor is called when the CLR checks that there are no more references to the object, and then will eliminate it from memory. If there is a destructor declared in the object class, then it will be called to "clean up" the object, before the memory is returned to the system.

Now, speaking of your example, you’re not making much sense. Usually in the destructor is the cleaning of non-Managed resources... but in the example is being created a new object, which may even make sense seen the full context, but it is in a way a very code "suspect".

  • Thank you very much for the explanation

  • This I learned in college, rs, I thought I would not take anything in the life of the things I learned in C, but I see that is not so XD

16

This is a destructor. The syntax is inherited from C++. But the semantics are quite different.

In practice destructors do not exist in language. To understand I will explain two other concepts.

Dispose()

This is a method available in all types that implement the interface IDisposable and allows resources to be released when the object is no longer needed. The Dispose() can be done manually at any time or automatically using the statement using. The release of resources are free memory. Only the Garbage Collector allows to free up memory. If a type does not need to have external resources, it is unnecessary.

Finish

This is the method that actually processes everything that is necessary in the moment before the release of memory. We can say that he is responsible for liberation, although actually who does this is the Garbage Collector. A finalize() is always available, because he is part of the type Object and in most cases the standard implementation of Object is enough.

Destructor

You can’t override a method Finalize() on its type. The compiler does not allow you to use your own implementation. But it allows the destructor to be created. And he can process what he wants in the destructor and call the Finalize(). The standard implementation of Finalize needs to be available in all types.

So the code

class Car {
    ~Car() { // destrutor
        // código de limpeza...
    }
}

is the same as

protected override void Finalize() {
    try {
        // código de limpeza...
    } finally {
        base.Finalize();
    }
}

but the second way can only be generated by the compiler in place of the destructor.

Therefore a destructor in C# is neither executed soon after the object is no longer needed, nor is it executed in the same thread and yes in the thread of Garbage Collector. There are a number of problems related to the finalizing method that are not part of the question’s scope. Finishers, even if in destructor form should only be customized when the programmer deeply understands all its implications.

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.