Five Minute C# — Lesson 6

Functions, classes, namespace, and access modifiers

Alexander Laurence
Five Minute C#
6 min readOct 28, 2018

--

Lessons: 12345678910 ・ 11 ・ 12 ・ 13 ・ 14

Reusing your own code

We’ve written a lot of meaningful code, but we missed out one major part of the object-orientated methodology: functions (or methods)!

Functions/Methods

A function is a reusable section of code which executes only when called upon. Think of it as a way of packaging bits of code in a neat way, and when it is called upon, it will return a value based on what you’ve written for the function (unless you void it!).

For example in Unity, we have a function which executes as soon as the application starts called Start(). Since we just want to execute bits of code and not to return a value, it is voided.

void Start()
{
//any code written here will run immediately as the program starts
}

There’s also an Update() function too. It works in a similar way to the Start() function.

void Update()
{
//any code written here will run constantly
}

These are just examples of the stock functions written in the Unity engine. But what if we want to write our own function? No problem!

Let’s make a function that tells the console to print “Hello”. We’ll define our function as SayHello().

void SayHello()
{
Console.WriteLine("Hello");
}

Classes

It’s important to note that every function exists within a class. Let’s call our class “Talk”. It should look like this:

class Talk
{
void SayHello()
{
Console.WriteLine("Hello");
}
}

You can have more than one class in your script! Let’s create another class called “Listen”.

class Talk
{
void SayHello()
{
Console.WriteLine("Hello");
}
}
class Listen
{
//do other stuff
}

Namespace

All classes exist within their own namespace. Usually this is your project name. Let’s call our project “MyFirstProgram”.

namespace MyFirstProgram
{
class Talk
{
void SayHello()
{
Console.WriteLine("Hello");
}
}
class Listen
{
//do other stuff
}
}

Nice! There can only be one namespace. Now we have a proper script!! Remember, if you see a method or function outside of a class, you will get an error. Likewise, if you see a class outside of a namespace, you will also get an error. So please pay extra attention to how you write your code. It should always follow the same hierarchy as the above example.

Abstraction and Encapsulation

OK now that we have explained that elephant in the room, we can return back to methods. Let’s create a function for the Listen class, since it has nothing and it’s feeling a bit lonely…

We’ll create a new function called HearWords()…

namespace MyFirstProgram
{
class Talk
{
void SayHello()
{
Console.WriteLine("Hello");
}
}
class Listen
{
void HearWords()
{
//Do something
}
}
}

Let’s make HearWords() reuse our SayHello() function that we already wrote, instead of rewriting its content again.

namespace MyFirstProgram
{
class Talk
{
void SayHello()
{
Console.WriteLine("Hello");
}
}
class Listen
{
void HearWords()
{
SayHello();
}
}
}

Bingo! That’s how you call your own function… But wait… We’re getting an error. What’s happening?

Since SayHello() exists in a completely different class to HearWords(), it requires permission to access its contents. This is called an access modifier (or protection level), and there are 3 main ones which we will go over: public, private, and static.

By making SayHello() exposed as a public function, HearWords() will be able to access it from the Listen class, and will therefore return its value as expected.

namespace MyFirstProgram
{
class Talk
{
public void SayHello()
{
Console.WriteLine("Hello");
}
}
class Listen
{
void HearWords()
{
//this will work
SayHello();
}
}
}

So why didn’t it work before? Well, by not specifying it’s protection level, your function reverts by default to private. There is no difference from writing private void SayHello() or just void SayHello(). But you can write private for the sake of consistency. It’s also good practice when things get a bit more complicated with further protection levels (which we may go over in a future lesson!)

So if we make SayHello() a private function, it can no longer be accessed by HearWords().

namespace MyFirstProgram
{
class Talk
{
private void SayHello()
{
Console.WriteLine("Hello");
}
}
class Listen
{
void HearWords()
{
//this will not work
SayHello();
}
}
}

But then, what’s the point of making functions private? Well, think of a public function as a contractual agreement between the function and the class about what it is exporting to the world. By making slight changes to the public function, you risk breaking the contract that the rest of the world is assuming about the class. This naturally can lead to bugs, and so it is generally good practice to make functions private unless proven otherwise. This is known as encapsulation, and is a major philosophy of object oriented programming.

OK, so what about the static protection level? While public functions have no restrictions with access and private functions are internal to the class, static functions are something that lives logically within the realm of the class while not depending on the state of the class.

We’ll turn SayHello() into static function.

namespace MyFirstProgram
{
class Talk
{
static void SayHello()
{
Console.WriteLine("Hello");
}
}
class Listen
{
void HearWords()
{
//this will not work
SayHello();
}
}
}

Since SayHello() is a static function, it is a characteristic of the class Talk. Therefore, Objects created by Talk will not have a SayHello() function. But above all, there will only be one instance of SayHello().

In general, this is to be used sparingly for important functions. For example, Console.WriteLine(string) is an example of a static method.

Interestingly, a static access modifier can be either public or private. This is useful if you have multiple script files and you want to call a static function from one namespace to another. You can do this by referencing. A good example of this is the extension method!

Firstly, turn your static function into a public static function.

namespace Script1
{
class myClassName
{
public static void myFunction()
{
Console.WriteLine("This is my Function");
}
}
}

Now you can reference it anywhere, as long as it fits with the syntax.

namespace Script2
{
class someOtherClass
{
void Start()
{
Script1.myFunction();
}
}
}

The idea of exposing certain necessary things (by using static) is called abstraction in object orientated programming system principle. While the opposite, hiding uncessary things (by using private) from the “outside world” is called encapsulation.

Finally, you can apply this knowledge of access modifiers to variables too. They will work in a similar way to functions as protection level is concerned. Here’s some examples:

public int cakes;
private float slices;
static string feedback;

I’ve thrown you completely into the deep-end but I hope by doing so, you will have a much more better understanding of what’s coming up ahead.

That’s it!

--

--

Alexander Laurence
Five Minute C#

Alex graduated from the University of Edinburgh with a Masters degree in Neuroscience. He spent 3 years teaching and now runs his own tech venture.