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.
In the previous sections, methods 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.
When discussing variables, their various different possible data types were listed. These were int, string, 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.
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?
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.
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
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
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.