Tuesday, November 24, 2009

Final, Finally, Finalize or Sealed?

Final access modifier
  • Final can be used for classes, methods, and variables
  • Final variables can't change from the initialized values
  • Final classes can't be subclassed
  • Final methods can't be overriden
  • C# uses the keyword "sealed" instead of "final" for classes and methods
  • A class or method can be "sealed"
  • A "const" may be used for equivalent of a "final" variable

Finally block
  • Finally is used in exception handling
  • Finally is a part of the try-catch-finally
  • Finally is executed even if no exception is thrown
  • Read more on finally in this post

Finalize() method
  • Finalize() is a non-explicit way to clean up resources
  • Memory resourses for the object are cleaned up using Finalize()
  • Finalize() is called during garbage collection to allow objects to clean up unmanaged resources
  • Finalize() should always be protected
  • Finalize() should release unmanaged resources only

No comments:

Post a Comment