Professional Documents
Culture Documents
07 Handout 1 (16) (C#)
07 Handout 1 (16) (C#)
More on Classes
I. Destructors
A programming concept, along with constructors, that is commonly used for classes and
objects.
They are automatically invoked when an object is destroyed or deleted (as opposed to how
constructors are invoked when an object is created).
Their names are exactly the same as their parent class, except they are prefixed with a tilde
(~) before their name to denote that they are destructors.
They can have the following attributes:
o ONLY one (1) destructor is allowed for a class.
o Destructors CANNOT be called, as they are invoked automatically.
o A destructor DOES NOT take modifiers or have parameters.
A. Static Methods
They are methods that use the same concept as with static member variables; they also
use the static keyword for their declaration.
These methods can only access static members. For example, the Main method and any
method called directly to it is automatically a static member.
B. Constant Members
These are constant values (usually variables) that are used within static methods (such
as days of the week, the value of Pi, a generic greeting, etc.).
These members are static by definition; the static keyword is replaced by the const
keyword in their declaration.
C. Static Constructors
These are constructors declared with the static keyword.
They can be used to initialize static members of a class.
They are automatically called once when a static member of the class is accessed.
V. Indexers
A programming concept in C# that allows objects to be indexed similar to elements of an
array.
It is written similar to a call statement for an array: a pair of square brackets ([ ])
containing the index number of the object.
A. Indexer Properties
Indexers can be declared as something similar to a property, with the only difference
being that indexer get and set accessors require an index number.
They are defined using the this keyword.
Indexers return or set a particular value (compared to how a property returns or assigns
data members) from the object instance; this allows indexers to hold multiple values
like an array using fewer instantiated objects.
References
Microsoft. (n.d.). Introducing Visual Studio.NET. Retrieved on May 8, 2018, from
http://msdn.microsoft.com/en-uslibrary/fc6bk1f4(v=vs.71).aspx.
SoloLearn. (n.d.). C# tutorial. Retrieved on July 17, 2018, from
https://www.sololearn.com/Course/CSharp/