0

In this example link ....

https://stackoverflow.com/a/19737565/2948523

I found some sections like, ~ImprovedClass() ~Inner()

Please help me out what are they ? why and how should I use them in code

            class Inner
            {
                public Inner(IntPtr unkOuter)
                {
                }

                ~Inner()
                {
                }
            }

        public class ImprovedClass 
        {
            // constructor
            public ImprovedClass()
            {
            }

            ~ImprovedClass()
            {
            }
        }
Community
  • 1
  • 1
Dipon Roy
  • 396
  • 1
  • 4
  • 18

3 Answers3

3

This is a Destructor Destructors

They are used to release resources that the object may still be holding onto even though it is no longer in use.

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
2

Those are called destructors which are calling automatically at the end of your class instance life on instance. You can write code here to release some resources which have been used by your object. Here are some remarks about destructors:

  1. Destructors cannot be defined in structs.
  2. They are only used with classes.
  3. A class can only have one destructor
  4. Destructors cannot be inherited or overloaded.
  5. Destructors cannot be called. They are invoked automatically.

Here is some guide http://msdn.microsoft.com/en-us/library/vstudio/66x5fx1b.aspx

Arsen Alexanyan
  • 3,061
  • 5
  • 25
  • 45
0

~ mark is used for destructor not the constructor.

When you are using unmanaged resources such as handles and database connections, you should ensure that they are held for the minimum amount of time, using the principle of acquire late and release early. In C++ releasing the resources is typically done in the destructor, which is deterministically run at the point where the object is deleted. The .NET runtime, however, uses a garbage collector (GC) to clean up and reclaim the memory used by objects that are no longer reachable; as this runs on a periodic basis it means that the point at which your object is cleaned up is nondeterministic. The consequence of this is that destructors do not exist for managed objects as there is no deterministic place to run them.

Thilina H
  • 5,754
  • 6
  • 26
  • 56