Types, Methods, and Classes

Intermediate topics around types, methods, and class extensions to enhance the functionality of the code created.

Lets talk about some intermediate C# topics that will help in furthering my game development skills. Access modifiers not only consist of public and private. There are many more that can be used in your programming; const, readonly, static, abstract, and override.

The const or constant access modifier is used to create a variable that stores a constant unchanging value. This would be useful for variables that will never change in game. Things like setting a win condition that the variable can be tested against like:

public const int MaxItems = 4;

The line of code above locks the MaxItems variable to 4 and can never be changed. The biggest issue with const variables is that they need to be assigned a value at their declaration. If we want to declare a variable that is unchangeable but don't want to assign a value just yet, that is where readonly comes in:

public readonly int MaxItems;

The readonly keyword is used to declare the variable while allowing its assignment at a later time. You can use the Start() or Awake() methods to assign the value.

The static keyword is used for classes that don't need to be instantiated and not all of the properties need to belong to a specific instance. The gotcha of the static keyword is that static classes cannot be used in class inheritance. An example of a static class would be a utility method where instantiating the method isn't needed because the methods used don't depend on a particular object.

There is a way to create multiple methods using the same name but have different signatures. This is called method overloading. In method overloading the method is modified by changing the number of parameters or the type of parameter while still keeping the same method name. Method overloading adds flexibility when you need to have more than one option for a given operation.

In coding, there are situations where you may want to pass in a method argument by reference so that changes made are reflected in the original variable. This is done using either a ref or out keyword. To use ref in code there are a few rules:

  1. Arguments have to have been initialized prior to being passed into the method.
  2. Initialization or assignment of the reference parameter isn't needed prior to ending the method
  3. Properties with get or set accessors can't be used as ref or out arguments

Similar to ref, the out keyword does similar work but with a different set of rules. They are not interchangeable.

  1. Arguments do not need to be initialized before being passed into a method
  2. Referenced parameter values do need to be initialized or assigned in the calling method before its returned

OOP... You know me

Advancing C# knowledge requires diving head first into object oriented programming and its applications. One way to collect groups of functionality together is through the use of interfaces. Interfaces are similar to classes but have one big difference, interfaces can't have any implementation logic or stored values. Interfaces contain the implementation blueprint leaving the adopting class or struct to fill in the values and methods listed in the interface.

Another approach to separating blueprints is through the use of abstract class. Abstract classes can't contain any implementation logic for methods but may contain variable values. If you need to set initial values, abstract class should be used over an interface.

Above, I've talked about creating custom objects but what if you just need to extend an existing class? This is done through class extensions. To extend a class there are some rules. Classes can only be modified with methods, no variables or other entities are allowed. The syntax looks like:

public static void FancyDebug(this string str)

A class extension needs to be marked as static, the first parameter is always the 'this' keyword followed by the name of the class that is being extended, and a local variable name.

I haven't gotten very far in my journey just yet but when I get to a place where I'm seeing namespace conflicts I know that type aliasing is the way to handle them.

using CustomInt = System.Int64;

The above call allows me to use the Int64 name space by calling it by its aliased name of CustomInt.

Wew, thats a lot of intermediate stuff in the chapter. Amazingly enough, I'm understanding quite a bit about the topics outlined in these more advanced chapters. I'm really glad I found this book.