C Sharp Basics: Part 3: Methods

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.


Methods

Screenshot 2018-10-10 11.31.12

In the previous sectionsmethods were introduced as sets of instructions. Like, variables within a class, they can be public or private. Depending on this category, they can be accessed either exclusively inside the object or from the outside using its name.

Methods Return

Screenshot 2018-10-10 11.36.14

When discussing variables, their various different possible data types were listed. These were intstring, and many others. Methods, as sections of code, have a similar definition. Instead of what type of data they are, like variables, these types define what they return.

As a set of instructions, methods can perform actions on data. They can change, update, or remove data from an object. In defining them, a method needs a return type. When writing methods, they are, then, public or private, have their return type, and then have their name.

screenshot-2018-10-10-11-39-28.png

The return type of a method must match the use of the return keyword within it. The use of the return keyword also “returns” execution back to the whatever called this method. With the return keyword, the method ends.

What about void?

Screenshot 2018-10-10 11.43.40

Like in C# and some other programming languages, the Main() method in this example uses a return type of void. This is a special keyword that does not return anything. In the cases of using void, a method will run through its instructions and then stop. Instead of a return passing control back, the method simply ends.

Constructors

Screenshot 2018-10-10 11.47.46

There is a special kind of method called a constructor. This, as it name implies, “constructs” an object. A constructor is a method with the same name as the type (class) in which it is found. This is the method called when a new object of the type is created. This allows for passing data into the object when it is created to setup initial values.

This is me

Screenshot 2018-10-10 11.51.46

An object can refer to its variables and methods through the keyword this. Using the keyword allows for an object to reference itself. When using this, it always refers to the object in which it is found and uses the name of the variable or method within that object.

Get and Set

Screenshot 2018-10-10 12.12.16

Getting and setting values is so common in programming that C# uses two special keywords, get and set, to help organize these tasks. Instead of writing a separate method to get a value to change some private data and another method to set some data, these can be combined into a single, special type of method that uses the get and set keywords.

Although it can appear more complicated, using get and set reduces the overall complexity of writing code of two very common actions. They also allow for changing how these actions can work. Instead of simply accessing some data, as with a variable, it can be changed to return a different type, a computed value, or perform some type of actions before returning the data.

Play with the example on Repl.It!