-3

So i bumped in to this:

Public Stam {
       public Stam() {Console.WriteLine("Stam");}

       ~Stam(){Console.WriteLine("Stam")}
       }

What exactly is the ~? and what will happen when i call the Stam class.

Bodokh
  • 976
  • 4
  • 15
  • 34
  • 2
    Finalizer. And you don't call classes. – ta.speot.is Feb 20 '14 at 11:20
  • @ta.speot.is When i mean call class i meant referring to the class or having the initialize do something – Bodokh Feb 20 '14 at 11:21
  • It's a finalizer/destructor. If one occurs in your code, you almost certainly did something wrong. – CodesInChaos Feb 20 '14 at 11:21
  • is this what you are looking for? [link](http://stackoverflow.com/questions/188688/what-does-the-tilde-mean-in-c) – Richnau Feb 20 '14 at 11:21
  • Destructors in C# are denoted with ~ oeprator. – Amit Feb 20 '14 at 11:22
  • 1
    Please see this answer by one of the guys who has worked on the development of C#: http://stackoverflow.com/a/4899622/2920343. Especially note the first two words ("When should I use a destructor?"): "Almost never" followed by a bunch of warnings. – CompuChip Feb 20 '14 at 11:23

4 Answers4

0

The ~ operator, in this circumstance, is being used to denote the class destructor, destructors are called automatically as part of the cleanup process.

However, the ~ can also be used for bitwise complement operation.

James
  • 80,725
  • 18
  • 167
  • 237
0

It is a destructor for the class. It is called automatically when an instance of the class is deleted, you use it to delete objects etc.

Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
0

In C# it's called Destructor, the equivalent to a C++ destructor is IDisposable and the Dispose() method, often used in a using block.

See System.IDisposable from MSDN

What you are calling a destructor is better known as a Finalizer.

Guilherme Oliveira
  • 2,008
  • 3
  • 27
  • 44
0

generally destructor function/methods are declared like this.

Your call seems like a finalizer.