C# Introduction

C# Introduction

.NET C# is a powerful language that stems from C, which is the mother of all languages. In the coding world, C is the global language that is the base for all other languages. Learning C# is a great learning point for anyone wanting to pursue higher level coding or go deep into low level devices.


This lesson assumes you have completed the C# System Setup and have created a Visual Studio C# Project.

A reminder to add this line of code to all code samples.

using static BrainPad.Controller;


This lesson provides a quick introduction to the language with links to individual sub-lessons. The goal at the end of this lesson is to write a program that counts 0 to 7. And then we’ll modify the program to print 0 to 7 but skip number 4.

Case Sensitive

C# is case-sensitive like most coding languages, but what does that mean?! This means the language cares greatly on how things are spelled. For example, Print() and print() are not the same. The first one is the Print() function we have used earlier, but the second one will cause an error because there is no print() function built in, not unless you have made one!


A comment is a group of words or sentences we can add to our code that don’t affect how it runs. The computer will ignore the comment and run the code only. We use comments to tell us what’s happening in the code we’ve written or how something is done so others can understand the code we’re creating.  Most languages use // to start a comment and everything after that // will be ignored.

To make comments easier to spot by our eyes, they are usually colored differently.

Comments can also be useful for testing different sections of code by commenting and uncommenting lines and viewing the results.

using static BrainPad.Controller;
// This is a comment. 
// Comments are useful to remind us of what our code is doing. 
// The computer will ignore comments completely.

// Print("Goodbye"); //This line of code won't run unless we uncomment it. 

C# also supports block comments, which start with /* and end with */. But we will not be using them in these lessons.


A variable is a spot in the computer memory to help the program in memorizing something. We can give the variable any name, but the name can’t contain spaces, can’t start with a number, and can’t contain a symbol. Say we have a variable to keep track of a count and the count now is 5.

var count = 5;

Can you guess why Print() showed the value of count, which is 5, instead of printing the word count? Adding double-quotes around text tells C# you need the actual text processed; otherwise, it is a variable.

var count = 5;
Print("count = " + count);

Let’s make a program that takes two numbers, and then adds them up. The two numbers will be stored as variables a and b.

var a = 5;
var b = 4;
Print("The sum of a and b is:");

The Variables lesson covers further details.


So far, all of our programs have executed a small program and then terminated. However, most programs will continue to run, handling specific tasks repeatedly. This happens in a loop that executes a specific block over and over.

To indicate a block, C# uses curly brackets {  }. In this example, the block is a while loop that will run as long as the variable count is less than 8. Inside our block, we increment count by 1 in every loop. We do so by taking the count value, add 1 to it and then assign the result back into count. So, if count was 3 before that line, we add one to it making it 4 and then assigning it back to the same variable itself.

var count  = 0;
    count =  count + 1;

Learn more in the Loops Lesson.

White Space

White space does not mean anything to C#, making it optional. White space does make the code easier to spot though. The following two code examples are both identical and are both completely valid. Which one would you rather use?

var count  = 0; while(count<
    Print(count);    count =  
count + 1;}
var count  = 0;
    count = count + 1;

Flow Control

The while-loop used earlier automatically checked for a condition and kept looping as long as that condition was true, which was count is less than 8 (count<8). But what if we want to check for something without using a while-loop? This is exactly what you may guess, an if statement. You simply have a block, like the loop, but in this case the block will run only once if a condition is true.

Let’s say we want to print numbers 0 to 7, but this time we want to skip 4! Meaning we will Print() the number if count does not equal 4. In most programing languages, “not” is the ! symbol. And the != means “does not equal”.

var count  = 0;
    if(count != 4){
    count =  count + 1;

The code is same as before, except we check the count before we print. And we only print if the count does not equal 4.

Learn more in the Flow Control Lesson.


Operators are used for assigning values, like when we used = symbol, and for evaluating/comparing two values, like when we used the > symbol. There are other operators to help us mathematically, like the + symbol, and others to help with logic checks.

Learn more at the Operators Lesson.


Functions are tasks a computer has learned to execute. When someone asks you to sleep, you wouldn’t just lay down in place and fall asleep. The “sleep” function (task) will internally handle many tasks, like go to bedroom, turn lights off, lay in bed, shut eyes…etc. However, instead of commanding all the above separately, you simply combine it all into “sleep”.

You can create your own functions that handle multiple tasks. Additionally, we have premade some functions for you. Our favorite is Print(), which we’ve already been using in the examples above.

By the way, we will always add () to indicate a function.

Print("Hello World!");

In C#, every line must end with a semicolon. Go ahead and try this code. You can change it to print your name, and even add multiple lines.

Print("Hello, my name is:");

The things we give Print() are called arguments. We have given it a string (text) so far, but we can give it a number as well, or even a mathematical calculation. See what happens in these examples:


You can make your own functions or use one of the available ones. The Functions Lesson covers functions in further detail. And the Built-in Functions page has a list of functions that are premade for you, just like the Print() function.


This is one of the greatest features in C# running on the BrainPad. It is in the fact that you can step through code and inspect variables on the device.

Let us go back to our previous program that counts to 8. Set a breakpoint at the Print() line. You can do so by hitting F9 key while being on that line or by clicking at the far left column.

When we run the program now, the C# engine will see a breakpoint at that line and will pause the program.

Hover over the count variable with the mouse pointer and Visual Studio will show you the variable current value.

Step over the code by clicking the step over icon or by using the F10 key.

Observe what the program is doing (step-by-step) and how the BrainPad reflects the Print() exactly when you step over it.

Visual Studio also includes a way to easily see variable values. There is a window on the bottom with “Autos” and “Locals” tabs that show variables and their values. Note that the window only appears when in debugging mode, like you paused a running program or the program stopped at a breakpoint.

All the above may seem unnecessary, and you are right. Your program is only 5 lines of code with one variable. A developer (you) can easily see what is happening on such a small program without debugging. Now imagine 100,000 lines of code with 100 variables and the program doesn’t seem to work as expected. This is where debugging becomes a must-have.

What’s Next?

This lesson links to individual lessons that cover each topic in further detail.

Once you understand the core concepts of C#, you can start with other Lesson Plans covering Robotics, Games, and Circuits.


Computers are not as smart as we may assume. They run instructions step-by-step. But if so, how does a computer play music while surfing the web and checking emails? They do so by running billions of instructions per second. It feels like things are happening simultaneously, but it’s just a very fast simple machine.

How do computers compare to our brains? Do you think we process one instruction at a time? How about feelings? Computers always have an answer, but how come we are sometimes unsure? How do we make decisions that we feel confident about, but then we regret those decisions later? Would a computer regret a decision? What is “regret” to a computer if all it knows is a yes or no?