C Sharp Basics:
- Part 1: Class Anatomy
- Part 2: Public and Private Variables
- Part 3: Methods
- Part 4: Thinking in Objects
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
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
When one object creates another inside of it, this is a has-a relationship. The parent “has” that object and is using it.
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
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.
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.
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!