Methods and Libraries
Computers instructions are very simple, meaning you need many instructions to perform a meaningful task. In order to work with this, programming languages allow you to group instructions into reusable packages called methods in C#.
Programming languages further organise code that you can use into Libraries. These are larger bundles of coding artefacts that you can use within your program.
In C# there is the standard System
library that contains methods organised into classes to perform different functions. For now, you can make use of the System.Convert
class to give you access to methods to convert data. We will also use the SplashKit library and the SplashKitSDK.SplashKit
class which contains methods you can use to draw graphics, play sounds, and interact with the user in the Terminal.
In your program you can access the methods in a class by adding a using static
directive to the top of the code. This tells the compiler you want to have access to the methods in a class.
This illustration highlights how your code can access methods.
Example
This program uses methods in the SplashKit and the C# libraries to create a test window that shows a random color for a period of time based on user input.
Activities
Here are some method names from the SplashKit library. What do you think these methods do?
WriteLine
OpenWidow
FillRectangle
PlaySoundEffect
Rnd
Answers
- 1:
WriteLine
writes a line of text to the terminal. - 2:
OpenWindow
opens a graphical window you can draw to. - 3:
FillRectangle
will color a rectangular area in the window. - 4:
PlaySoundEffect
will play a sound effect that you have loaded. - 5:
Rnd
returns a random number - its name it not as clear as the others. Ideally each method's name should capture what it does.