0% found this document useful (0 votes)
8 views

07 Handout 1 (16) (C#)

This document discusses several key concepts in object-oriented programming using C#: 1) Destructors are invoked automatically when an object is destroyed to clean up resources, while constructors are invoked during object creation. Destructors have the same name as the class prefixed with a tilde. 2) Static members belong to a class rather than individual objects. They use the static keyword and only one copy exists regardless of the number of objects. Static methods can only access static members. 3) Static classes contain only static members and cannot have objects instantiated. They are useful for grouping related properties and methods, like the Math class. 4) Indexers allow objects to be indexed like arrays using square brackets

Uploaded by

Gideon Arias
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

07 Handout 1 (16) (C#)

This document discusses several key concepts in object-oriented programming using C#: 1) Destructors are invoked automatically when an object is destroyed to clean up resources, while constructors are invoked during object creation. Destructors have the same name as the class prefixed with a tilde. 2) Static members belong to a class rather than individual objects. They use the static keyword and only one copy exists regardless of the number of objects. Static methods can only access static members. 3) Static classes contain only static members and cannot have objects instantiated. They are useful for grouping related properties and methods, like the Math class. 4) Indexers allow objects to be indexed like arrays using square brackets

Uploaded by

Gideon Arias
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SH1803

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.

II. Static Members


 These are classes or method members (variable or another method) declared within a static
class or method; they belong to that class/method, instead of any specific instance (such as
an object).
 They use the static keyword on their declaration.
 No matter how many objects of the class are created, there is only one (1) copy of
the static member.

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.

III. Static Classes


 These are classes that contain only static members.
 They cannot instantiate objects, as only one (1) instance of the static class can exist in a
program.
 These classes are useful for combining logical properties and methods. A good example of
this is the Math class, which contains various useful properties and methods for
mathematical operations.
 All members of the static class can be accessed using the static class’s name; objects need
not be declared to access them.

07 Handout 1 *Property of STI


[email protected] Page 1 of 3
SH1803

A. Static Methods and Properties of the Math Class


 Math.PI returns the value the constant PI (π).
 Math.E represents the natural logarithmic base e.
 Math.Max() returns the larger value of its two (2) arguments.
 Math.Min() returns the smaller value of its two (2) arguments.
 Math.Abs() returns the absolute value of its argument.
 Math.Sin() returns the sine of the specified angle value.
 Math.Cos() returns the cosine of the specified angle value.
 Math.Pow() returns a specified number (first argument) raised to the specified power
(second argument).
 Math.Round() rounds a decimal number to its nearest integral value.
 Math.Sqrt() returns the square root of a specified number.

B. Static Methods for the Array Class


 The Array class is a static class in C# that allows array manipulation through its
methods:
o The Sort() class sorts an array’s elements in ascending order of their values.
o The Reverse() class sorts an array’s elements in descending order of their values.

C. Static Methods for the String Class


 The String class is a static class that allows string manipulation through its methods:
 The Concat() method combines any strings given as an argument and returns the
combined strings as a new string value.
 The Equals() method determines whether any strings given as argument is equal;
the method returns the Boolean value of True if the strings are equal and False if
they are not.

D. Static Properties for the DateTime Class


 The DateTime class is a static class that allows manipulation of date or time values via
its properties:
o The Now property returns the current date and time; these values are usually derived
from the computer’s system clock.
o The Today property returns the current day of the week.
o The DaysInMonth property returns the number of days in a specified month of a
specified year.

IV. this and readonly

A. The this Keyword


 It is a keyword used inside a class that allows the referral to the current instance (object)
of that class.
 This allows a program to distinguish class members from other data, such as local or
formal parameters of a method.

B. The readonly Keyword


 It is a modifier keyword that prevents a member of a class from being modified after
construction.
 The member can only be modified during declaration of from within a constructor.
 Attempting to modify the member with the keyword will return an error.

07 Handout 1 *Property of STI


[email protected] Page 2 of 3
SH1803

C. Differences between readonly and const


 Members with the readonly keyword can be declared without an initialized value, while
constants (members with the const keyword) need to have an initialized value.
 Members with the readonly keyword can have their values changed in a constructor;
constants, on the other hand, cannot.
 readonly members can have evaluation values (values resulting from a calculation,
condition test, etc.); constants cannot.

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.

VI. Operator Overloading


 Most operators in C# can be overloaded, meaning they can be redefined for custom actions.
 This allows a programmer to change the actions of an existing operator in a custom class.
 This can be achieved via the operator keyword, directly followed by the operator symbol
whose actions will be changed.
 Similar to any other method, an overloaded operator has a return type for its updated
actions and a parameter list to define what actions must be done when the overloaded
operator is used.
 Finally, overloaded operators must be given a static access modifier in order for its updated
actions not to affect other operators in the program.

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/

07 Handout 1 *Property of STI


[email protected] Page 3 of 3

You might also like