What Does the Tilde Before a Function Name Mean in C#

What does the tilde before a function name mean in C#?

~ is the destructor

  1. Destructors are invoked automatically, and cannot be invoked explicitly.
  2. Destructors cannot be overloaded. Thus, a class can have, at most, one destructor.
  3. Destructors are not inherited. Thus, a class has no destructors other than the one, which may be declared in it.
  4. Destructors cannot be used with structs. They are only used with classes.
    An instance becomes eligible for destruction when it is no longer possible for any code to use the instance.
  5. Execution of the destructor for the instance may occur at any time after the instance becomes eligible for destruction.
  6. When an instance is destructed, the destructors in its inheritance chain are called, in order, from most derived to least derived.

Finalize

In C#, the Finalize method performs the operations that a standard C++ destructor would do. In C#, you don't name it Finalize -- you use the C++ destructor syntax of placing a tilde ( ~ ) symbol before the name of the class.

Dispose

It is preferable to dispose of objects in a Close() or Dispose() method that can be called explicitly by the user of the class. Finalize (destructor) are called by the GC.

The IDisposable interface tells the world that your class holds onto resources that need to be disposed and provides users a way to release them. If you do need to implement a finalizer in your class, your Dispose method should use the GC.SuppressFinalize() method to ensure that finalization of your instance is suppressed.

What to use?

It is not legal to call a destructor explicitly. Your destructor will be called by the garbage collector. If you do handle precious unmanaged resources (such as file handles) that you want to close and dispose of as quickly as possible, you ought to implement the IDisposable interface.

putting a tilde in front of a method call?

It is the finalizer for the ViewModelBase class. It is called by the garbage collector before collection.

It is not really very useful because:

a) Garbage collection does really work and you do not need to test it.

b) It tells you nothing about your code when this gets called during normal execution, because for the most part the Garbage Collector just does its own thing and collects when it determines that there is memory pressure.

For the most part it is OK to not worry about the Garbage Collector - only worry about it when you have a real problem.

Also experience tells me - avoid using the finalizer since you are never sure what state the rest of your program will be in when it is called.

What does the tilde (~) character do here

That is destructor. It takes care that all resources are released upon garbage collection.

What is a function that has a ~ before the function declaration?

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.

What does the tilde mean in an expression?

That is the bitwise complement operator, also known as bitwise negation.

What is a function that has a ~ before the function declaration?

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.

What does ~ (tilde greater than) mean in this Swift code?

I believe the original developer was using Swift custom operator for Thread Marshalling by iJoshSmith.

func ~> <R> (
backgroundClosure: () -> R,
mainClosure: (result: R) -> ())
{
dispatch_async(queue) {
let result = backgroundClosure()
dispatch_async(dispatch_get_main_queue(), {
mainClosure(result: result)
})
}
}


Related Topics



Leave a reply



Submit