-6

This post contains the following source code:

class Foo 
{ 
    private int x;
    private int y;
    public Foo(int x, int y) 
    {
        this.x = x;
        this.y = y;
        SideEffects.Alpha(); // Notice: does not use "this"
    }
    ~Foo() 
    { 
        SideEffects.Charlie(); 
    }
}
static class SideEffects
{
    public static void Alpha() { ... }
    public static void Bravo() { ... }
    public static void Charlie() { ... }
    public static void M()
    {
        Foo foo = new Foo(1, 2); 
        Bravo();
    }
}

What does ~ do?

burnt1ce
  • 14,387
  • 33
  • 102
  • 162
  • 2
    It specifies a class destructor. http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx – NWard May 07 '14 at 18:21

3 Answers3

3

It is used for Destructor in C# which implicitly calls Finalize method.

Destructors are used to destruct instances of classes.

The destructor implicitly calls Finalize on the base class of the object.

The programmer has no control over when the destructor is called because this is determined by the garbage collector. The garbage collector checks for objects that are no longer being used by the application. If it considers an object eligible for destruction, it calls the destructor (if any) and reclaims the memory used to store the object. Destructors are also called when the program exits.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • @Downvoter, care to comment ? – Habib May 07 '14 at 18:34
  • I think the question shows little research effort, should be closed as a dupe, and deleted. Your answer is correct and helpful, but it does not add anything significant to [the other answers](http://stackoverflow.com/questions/188688/what-does-the-tilde-mean-in-c), instead it just keeps this question from being deleted. Obviously, I mean no offense to you or the asker, I just don't think we as a community should answer questions that already have easy to find duplicates (googling "c# tilde method" takes you to the correct page), and instead should just politely point to the duplicate. – Gray May 07 '14 at 19:06
  • 2
    @Gray, downvoting on answer of duplicate question is [debatable](http://meta.stackoverflow.com/questions/252009/should-there-be-a-deterrent-for-answering-obvious-duplicate-questions). But thanks for the clarification, I was just concerned if my answer is wrong technically. Thanks – Habib May 07 '14 at 19:08
  • 1
    That was a helpful discussion; thank you for the link. I guess I would say that this question teeters the line of being an egregious duplicate just because I would be very surprised to find a question like "what does the ___ operator do?" to not already have an answer. It just seems too basic. Someone made a good point about not punishing the answerer for the OP's mistake, but I feel like I want to encourage super helpful people like yourself to spend time on questions without an answer. Either way, I'd undo my downvote at this point if I could just because the link made me question my ideals. – Gray May 07 '14 at 19:23
  • 2
    @Gray, Google returns strange results if search for "C# ~ operator", for a guy like me who has English as third language, It is sometimes difficult to search for the exact term like "tilde". Anyway, I have voted to close the question and delete as well :) – Habib May 07 '14 at 19:28
1

It is the symbol for the C# Destructor. There's a detailed answer here.

Community
  • 1
  • 1
gabsferreira
  • 3,089
  • 7
  • 37
  • 61
1

The ~ symbol also is used to declare destructors says the ~ Operator (C# Reference)

Dr.Jukka
  • 2,346
  • 2
  • 15
  • 20