Five Minute C# — Lesson 1

Syntax, strings, and console output

Alexander Laurence
Five Minute C#
5 min readOct 27, 2018

--

Lessons: 12345678910 ・ 11 ・ 12 ・ 13 ・ 14

What is C#?

C# (pronounced, C-Sharp) is a cross-platform object-oriented language built for Microsoft’s .NET framework (don’t worry about this for now). As it is a C-based language, it bears syntactic similarities with many other C-languages. So if you know either C, C++, or Java (or all three), then you will find learning C# to be a breeze! A bit like if you’re a Spanish speaker, you will find learning Italian to be easier than say a Swahili speaker (due to the linguistic similarities shared between Italian and Spanish). But a Swahili speaker is just as capable of learning Italian as anyone else — so don’t be put off if you aren’t versed in any of the C languages!

C#’s cross-platform support allows you to build applications for Windows tablet, desktop, phones, iOS, Android, and Linux. The skills you gain as a C# programmer provides you with a fantastic opportunity to create applications for many different platforms. This is one of the many reasons why learning the C# language is insanely fun!

How do I get started?

You can start right away by going on https://dotnetfiddle.net. Or if you want to build actual applications, I recommend downloading Microsoft’s Visual Studio (it’s free), or Unity (if you plan on developing games). This should provide you with everything to start building your own programs. However, this course isn’t meant to be a tutorial. It’s merely here to provide you with a theoretical framework to understand some major concepts in C# (without the gobbledygook!). Now let’s get stuck in…

Lesson 1

So earlier I mentioned that C# is an object-oriented programming language. But what does that mean exactly?

Now, let’s make our first C# program!

In a broad sense, programming is a way of telling a computer a set of instructions. The easiest way to demonstrate this is through the console (a method of interacting with the computer). If we run the following:

Console.WriteLine("Hello World!");

This will tell the program to print “Hello World” to the console window. So the output will look like this:

Hello World!

But make sure you get the syntax correct, otherwise you will get errors.

Console.WriteLine("Hello World!');

Can you spot what we did wrong? If you run this program, you will get what is known as a compilation error. This is because the program has failed to compile your code as you have made a mistake in the syntax (used ’ instead of ”).

So make sure you pay extra attention to your syntax! Even experienced programmers spend hours figuring out what went wrong, only to discover they missed out a semicolon…

Nice! You’ve built your first C# program. But that’s too easy, right? Let’s dive into strings.

Remember the object orientated programming method? According to this, a text ‘object’ is referred to as a string. So when we told our program to write the “Hello World!” line, what we were really telling it was to display a literal string (i.e. “Hello World!”) in the console window by means of the Console.WriteLine(string) method.

Often in programming, we build systems which make use of data which change over time. For example, the number of students present in class, the most valued player of a football match, the location of the plane at the airport, etc. For this we use variables.

With this in mind, let’s play around with string variables. I want to create a program that will ask my name, and when given the answer, it will reply back with my answer.

Let’s make the program ask the question first:

Console.WriteLine("What is your name?");

Then, we’ll declare a variable which we call name as a string. We’ll make name call the Console.ReadLine() method in order to grab the user input. Our code will look like this:

string name = Console.ReadLine();

This will read what the user has typed, and store the data into the string called name. Notice the syntax, we declare the object type first (string), then the variable name (name) then we use the equals operator (=) to call Console.ReadLine() (method). The ordering is very important.

So far here’s what our program looks like if we run it:

What is your name?
|

Now let’s make the program reply back with out answer. We can do this by joining two strings together.

Console.WriteLine("Ah. So your name is " + name);

This is called concatenation. By concatenating “Ah. So your name is ” with name, we have made our program a bit more dynamic! Here’s the output of our program (let’s input my name Alex):

What is your name?
Alex
Ah. So your name is Alex

In our program, we defined a string by a method to get the user input:

string name = Console.ReadLine();

This is perfectly fine, but we could also define it with text.

string name = "Alex";

Of course, you can define your strings in any way you like.

string name;
name = "Alex";

That’s all for this lesson!

--

--

Alexander Laurence
Five Minute C#

Alex graduated from the University of Edinburgh with a Masters degree in Neuroscience. He spent 3 years teaching and now runs his own tech venture.