C Sharp Basics: Part 4: Thinking in Objects

C Sharp Basics:

C# is a multi-paradigm programming language created by Microsoft as part of the .Net initiative in 2000. It has gone on to have its own standard and become part of everything from desktop applications to used extensively in game development.


Thinking in Objects

Screenshot 2018-10-10 23.32.24

While C# can support different paradigms of programming, one of the most common is object-oriented. In this paradigm, different parts of a project are represented as objects that exist as part of two different types of relationships: has-a or is-a.

When different code is part of the same project in C#, it also needs to be organized. To do this, a namespace is used. This lets C# know that code, even though it is part of other files, is part of the same group of objects.

With namespaces, groups of objects can be separated into their individual files and use their has-a or is-a relationships to define themselves in reference to other objects.

Has An Object

Screenshot 2018-10-10 23.39.30

When one object creates another inside of it, this is a has-a relationship. The parent “has” that object and is using it.

Screenshot 2018-10-10 23.40.54

The example of creating a Person and giving it some initial values in order to display its values using the method Console.WriteLine() is a has-a relationship. The parent object, MainClass, “has” the object Person. Because both Person and MainClass are in the same namespace, MainClass can use and “have” a Person object.

Is An Object of Some Type

Screenshot 2018-10-10 23.44.17

The other type of relationship when describing objects is a is-a one. This often occurs alongside another concept in object-oriented programming: inheritance.

In C# (and other programming languages), an object can inherit from another. This allows for describing one type of generic object, a Person, for example, and then creating a more specific version, a Programmer, that is-a Person and can use its same members.

Inheritance happens in C# when defining a class. After its name, if a colon and the name of another class follows, this signifies that it is a child of that class. Inheritance works with a parent and child model: the original class is the parent and the new class is a child of it.

Screenshot 2018-10-10 23.51.15

The is-a model and the concept of inheritance allows for writing one set of code and using it again in another class. The difference, because it is a child object, is that any values passed it to as part of its constructor method need to also call the method base() and pass it the data it expects.

Because, in this example, the class Programmer is-a Person, it will have the same variables and methods. It can also, beyond what it inherits, also add its own variables and methods as well.

Screenshot 2018-10-11 00.06.31

Because Programmer is part of the same namespace as MainClass, it can “have it” as well. A Programmer object can be created inside the MainClass. The relationship to the object Programmer, then, is that it is-a Person, but that MainClass is in a has-a relationship with both Person and Programmer.

Play with the example on Repl.It!