Lessons usually are about BrainPad C# coding, but this lesson will show how the same knowledge can be leveraged to code a PC using the same C# and same Visual Studio!
Prerequisite
This lesson assumes you already know the basics of C# coding and know how to start a C# project.
Console Projects
We have used TinyCLR OS targeted C# projects with the BrainPad. On a PC, programs have two main types: Windows applications and Console Applications. The Windows applications are more common as they use “Windows” and provide a user interface. The other type is a console project, and it is a program that runs without a user interface.
The BrainPad does not have a “user interface”, and so we will use a Console application to start with something similar.
Open Visual Studio and create a new Console Application. If you can’t find it, just search “console application” in the top search bar.

The next window will ask for the project’s name and location. Give it any name you like and click “Next”. This is where you will be asked to select the target framework. We will select .NET 5.0.

In the Solution Explorer we will open Program.CS and modify the code to just have these lines. The first line will bring in the “System” library/services, and then the one line of code will “Write” to the console.
using System;
Console.WriteLine("Hello World!");
Run the program just like you would on the BrainPad.

A black window will appear displaying “Hello World!”. Press any key to close the window.
Print to Console
The BrainPad relied on the built-in Print function to print to the screen. We have the options to create a similar function that runs on the PC.
using System;
void Print(object o) {
Console.WriteLine(o);
}
Print("BrainPad");
Print(5);
Same Code!
Can we now bring some code from the C# Intro lesson and try on the PC? The core code is exactly the same. Only a few lines are needed to direct the system to use a library vs another.
using static BrainPad.Controller;
//------------------------------
var count = 0;
while(count<8){
if(count != 4){
Print(count);
}
count = count + 1;
}
using System;
void Print(object o) {
Console.WriteLine(o);
}
//------------------------------
var count = 0;
while(count<8){
if(count != 4){
Print(count);
}
count = count + 1;
}
This will run on BrainPad like we did before.

And also will on the PC.

Similarly, we can also create a Wait() function that uses the PC’s built in Sleep() function, which comes from the Threading library.
using System;
using System.Threading;
void Print(object o) {
Console.WriteLine(o);
}
void Wait(double seconds) {
Thread.Sleep((int)(seconds * 1000));
}
var count = 0;
while(true) {
Print(count);
count = count + 1;
Wait(0.1);
}
The code never terminates due to the infinite while loop. We can simply close the console window to terminate the program or use Ctrl+C shortcut keys to “cancel” the program.
So, do we dare say you are now a PC coder?
What’s Next?
Try other programs and take control over the PC. Of course, you don’t have a buzzer or buttons, but you get the idea. The web is full of documentation and tutorials on C#. A quick search for “console beep” will reveal Console.Beep() functions and similarly, searching for “console read key” will reveal Console.ReadKey() function. By the way, this function blocks and waits for a key to be pressed. “Block” here means the functions will stop the program until it is done with something, which is when a key is pressed in this case. We want it to not block and only check for A and B keys. This is to mimic the A and B buttons on the BrainPad.
This program will increase the counter when A is pressed and decrease it when B is pressed. We have created a function called ButtonPress() that returns A or B if these keys were pressed but returns nothing (an empty string) otherwise.
using System;
using System.Threading;
void Print(object o) {
Console.WriteLine(o);
}
void Wait(double seconds) {
Thread.Sleep((int)(seconds * 1000));
}
string ButtonPress() {
if (Console.KeyAvailable) {
ConsoleKeyInfo key = Console.ReadKey(true);
switch (key.Key) {
case ConsoleKey.A:
return "A";
break;
case ConsoleKey.B:
return "B";
break;
default:
break;
}
}
return "";
}
var count = 100;
while (true) {
var key = ButtonPress();
if (key == "A") {
count = count + 1;
Print(count);
}
if (key == "B") {
count = count + 1;
Print(count);
}
Wait(0.01);
}
BrainStorm
While there is a learning curve to using C# and Visual Studio, the user/learner is prepped with everything needed to build all kinds of applications, even mobile applications and video game console games! Do you now see how the BrainPad can be a starting point to someone interested in coding professionally?