Showing posts with label destructors. Show all posts
Showing posts with label destructors. Show all posts

C# - Classes


You saw a class structure in the “Hello, C# World!” sample. In the C#, you define a class by using the class keyword, just as you do in C++. Following the class keyword the class name and curly brackets ({. . .}), as shown here:
class Hello
{
static void Main()
{
Console.WriteLine("Hello, C# World!");
}
}
Note: C# classes don’t end semicolon (;) as C++.
Once a class is defined, you can add class members to it. Class members can include constants, fields, methods, properties, indexers, events, operators, instance constructors, static constructors, destructors, and nested type declarations. Each class member has an associated accessibility, which controls the scope of the member and defines whether these members are accessible outside the class. 

Destructors

Whenever an object goes out of scope, it is destroyed. The memory used by that object is reclaimed. Just before the object is destroyed, an object's destructor is called to allow any clean-up to be performed.
A dectructor, as the name suggests, is used to destroy the objects that have been created by a constructor. Like a constructor, the destructor is a member function whose name is the same as the class name but is preceded by a tilde sign(~). For example, the destructor for the class number can be defined as shown below:
~number() { }
A destructor does not take any arguments; LEARN MORE>>