Learning to Code — Part 8a: More on Classes

Scott Rosenbloom
Jul 31, 2018 · 7 min read

DESTRUCTORS

The SoloLearn app now goes into what, effectively, is the opposite of a constructor (which is the instantiation of a class), which is a destructor. Destructors are automatically invoked when an object is destroyed or deleted. They then list the following four attributes of destructors:

  • Destructors cannot be called. They are invoked automatically.
  • A destructor does not take modifiers or have parameters.
  • The name of the destructor is exactly the same as the name of the class, prefixed with a tilde (~), as in the following:
class Dog {
~Dog() {
//code statements
}
}
class Dog {
public Dog() {
Console.WriteLine(“Constructor”);
}
~Dog() {
Console.WriteLine(“Destructor”);
}
}
static void Main(string[] args) {
Dog d = new Dog();
}

STATIC

They keep mentioning the keyword static, and while I keep looking it up everytime I see it, they now go into a full explanation. Members of a class can be declared as static, meaning they belong to the class itself, as opposed to individual, object instances. No matter how many objects of the class are created, there’s only one copy of the static member.

class Cat {
public static int count = 0;
public Cat() {
Count++;
}
}
static void Main(string{} args) {
cat c1 = new Cat();
cat c2 = new Cat();
Console.WriteLine(Cat.count);
}
  • Within it, a member variable called count is declared, with a data type of integer, and an initial value of 0. This member is public, meaning it can be accessed by any other member within this class, or by another assembly that references it. Additionally, it is static, meaning it belongs to the Cat class itself, and not to any instantiation of the Cat class.
  • Also within the Cat class is a method called Cat which, when called, increments the current value of the variable count by 1.
  • Within the Main method, first a new object of the Cat type is instantiated with the name c1. When that happens, the method called Cat (within the class called Cat) is called, and the variable count is incremented from it’s current value of 0 to 1.
  • The next line within the Main method creates another new object of Cat type with the name c2. When that happens, the method called Cat (within the class called Cat) is called, and the variable called count is incremented from it’s current value of 1 to 2.
  • Then, printed to the screen, is the current value of the variable count, within the Cat class (which is 2).
class Dog {
public static void Bark() {
Console.WriteLine(“Woof”);
}
}
static void Main(string[] args) {
Dog.Bark();
}
  • If you’re calling another method from the Main method, which is not static, you need to instantiate it first (i.e. ClassName ObjectName = new ClassName();), and then call the method (i.e. ObjectName.NonStaticMethodName).

CONSTANTS

Lastly, the app covers constant members which are static by definition.

class MathClass {
public const int ONE = 1;
}
static void Main(string[] args) {
Console.Write(MathClass.ONE);
}
const string _html = “.html”;
_html = “”;

STATIC CONSTRUCTORS

On the same page, they go through static constructors which, when declared, can initialize static members of the class. The static constructor is automatically called once we access a static member of the class, like this:

class SomeClass {
public static int X { get; set; }
public static int Y { get; set; }
static SomeClass() {
X = 10;
Y = 20;
}
}

STATIC CLASSES

In the previous sections, we talked about individual members within a class being static. The SoloLearn app now goes through how an entire class can be declared as static as well. It’s important to note, however, that static classes can contain only static members. Also, you cannot instantiate an object of a static class, because only that one instance of the static class can exist within the program.

Console.WriteLine(Math.Pow(2,3));
  • Math.E — represents the natural logarithmic base e
  • Math.Max() — returns the larger of its two arguments
  • Math.Min() — returns the smaller of its two arguments
  • Math.Abs() — returns the absolute value of its argument
  • Math.Sin() — returns the sine of the specified angle
  • Math.Cos() — returns the cosine of the specified angle
  • Math.Pow() — returns a specified number raised to the specified power
  • Math.Round() rounds the decimal number to it’s nearest integral value
  • Math.Sqrt() — returns the square root of a specified number
int[] arr = {1, 2, 3, 4};
Array.Reverse(arr); //arr now equals 4, 3, 2, 1
Array.Sort(arr); //arr now equals 1, 2, 3, 4 once again
string s1 = “some text”;
string s2 = “another text”;
String.Concat(s1, s2); // combines the two strings
String.Equals(s1, s2); // returns false
Date.Time.Now; // represents the current date and time
DateTime.Today; //represents the current day
DateTime.DaysInMonth(2016, 2); // returns the number of days in the specified month
Console.WriteLine(); prints a value to the screen
Console.ReadLine(); gets user input
  • Indexers, which allows objects to be indexed like an array, and
  • Operator Overloading, which is the ability to redefine operators (like the plus sign) for custom actions.