This week we had a short session because of the fantastic demonstration that Colmac Robotics were kind enough to give us.
Following feedback from a few ninjas and ninja parents, I decided to do an impromptu session on the C# language. I had hoped that we’d perhaps just try to pick this up as we went along, learning through practical examples, but it was clear that the transition from visual languages to code-based languages was tough for some.
We used MonoDevelop to create a simple test application. Originally I had intended to use Visual Studio, but some people have had installation problems with it. I encourage everyone to re-install Unity at home when they have time, so that we can all standardise on Visual Studio in the near future.
Concept of Classes and Basic Structure of A C# File
I ran over these topics quickly again, but they were covered in last week’s post. Please do review it if you didn’t get a chance to read it before. It can be found here.
Simple Test Application
In our test application, apart from reinforcing the basic concepts mentioned above, we also touched on number of other concepts:
- We looked how the class definitions in our files are blueprints for how the class is structured and operates. We never actually have a copy, more commonly known as an instance, of that class until we actually create one ourselves with the new keyword.
- We discussed class constructors, the special method, with the same name as the class itself, that is called when we make an instance of that class.
- We saw how Console.WriteLine() can output text to the Windows console.
- We created a class called Shape. We said it was abstract; which meant that we would never be making an instance of the Shape class itself. We would purely be using it as a base for other classes.
- We said that Shape had a public method called Area(). We made its return type double, but we also marked it as abstract and provided no body for the method. This meant that we were saying all classes based on Shape would have to provide their own version of Area(), but that Shape itself had no definition for it.
- We defined a class called Circle. We made it so that it was derived from Shape.
- We gave it a property called Radius of type double and marked it public.
- We used the override keyword to make our own Circle specific version of Area(); “override” meaning that we were over-riding or replacing the method definition from the base class (Shape).
- We provided a means to set the Radius property through an optional argument to the constructor method Circle().
- We defined a class called Square. We made it so that it was derived from Shape.
- We gave it a property called Length of type double and marked it public.
- We used the override keyword to make our own Square specific version of Area(); “override” meaning that we were over-riding or replacing the method definition from the base class (Shape).
- We provided a means to set the Length property through an optional argument to the constructor method Length().
- When writing our Circle.Area() and Square.Area() methods, we saw how the built-in Math object has lots of useful mathematical properties and methods. We used the property Math.PI and the method Math.Pow() (to raise a number to a given power) to perform our area calculations.
In the end we had a small sample program that could defined instances of circles and squares, calculate their areas and output the results to the console window (command prompt window).
On-line Resources for Learning C#
If you want to brush up on your C# skills, outside of our CoderDojo sessions, there are a number of useful on-line resources:
- LearnCS.org: An on-line tutorial for C# which lets you build and test code right in a browser window
- tutorialspoint.com: Another on-line tutorial for C# which also lets you build and test code right in a browser window
- msdn.microsoft.com: Microsoft’s vast library of C# related resources including tutorials
Next week, we’ll be taking a few minutes to close off this sample application, so that we can quickly illustrate a few outstanding points, and then return to roll-a-ball to, hopefully, close it out. See you next week!