2. • A destructor works opposite to constructor, It destructs
the objects of classes. It can be defined only once in a class.
Like constructors, it is invoked automatically.
• Destructor is used deal locate the memory. it used to
closing the connections, closing the files. class name and
destructor name is the same but prefixed with ~(tilled) one
class supports only one destructor.
• Destructor is deallocate the memory. once we close the
application memory is deallocating. this concept based
garbage class. oops is nothing object oriented programming
system. we can gathered classes, properties, methods &
events
• Note: C# destructor cannot have parameters. Moreover,
modifiers can't be applied on destructors.
• C# Constructor and Destructor Example
3. • using System;
• public class Employee
• { public Employee()
• { Console.WriteLine("Constructor Invoked"); }
• ~Employee()
• { Console.WriteLine("Destructor Invoked"); } }
• class TestEmployee{
• public static void Main(string[] args)
• { Employee e1 = new Employee();
• Employee e2 = new Employee();
• } }
• Output:
• Constructor Invoked Constructor Invoked Destructor Invoked Destructor
Invoked
• Note: Destructor can't be public. We can't apply any modifier on
4. For more visit our website www.siri-kt.blogspot.com
Thanks for
Watching
More Angular JS TutorialsMore C sharp (c#) tutorials