10 min. read

How to begin with C#

Starting to program was never been this easier!
A guide to the very beginning of programming, written in a simple language.

Reasons to pick C#

  • It’s a fun and easy programming language.
  • It’s not hard! I know I said earlier it’s easy, but you need to know it’s not hard either!
  • It’s a professional choice - many high end games and network services are written using .net and C#.
  • It’s getting easier and easier, C# is a programmer-friendly language.
  • You can write games with it! That’s why I’m a programmer, You could be too!
  • Runs on many computers and systems!
  • You can do Machine learning, IoT services, Web-Apis, Web-Sites, Unity games, etc…

Installations

First of all we need some sort of a program to write our code and use it.
We can use Visual Studio Community Edition, it’s a good tool and free!
Install from here: https://visualstudio.microsoft.com/vs/community/

After picking the installation pick dotnet

Open your first project

First let’s pick a new project:

Secondly we’ll pick a console application under “.net core”

Pick names and a location

It doesn’t really matter what is the name in this project.

Basic layout of the IDE

An IDE is “Integrated development environment”.
Think of it as your desk with all kind of tools lying around.

Solution Explorer

A project groups couple of files to a single unit which will be "turned" into our program. This window will show you your projects and files.

Code files

Where your code files will be opened by default. This is where we will write our code.

Output

This will show the result of actions inside the IDE - the program we are using to write code.

We can press “Run” or press F5 and start the program

The result is shown:

How to give your friends your program

Open your project in the explorer (Folder view):

Go to “Bin -> Debug -> netcore…”
And you’ll see your files:

If you open youyr “exe” file, for exmaple mine is called ‘ConsoleApplication.exe’,
You’ll see it opens and closes, this is because the program is executing but nothing blocks it so it closes.
Let’s add in the code the line “Console.Readline();”

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;

namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Mars!");
Console.ReadLine();
}
}
}

Now it won’t be closed and you’ll see what you are written in the screen!

How to debug the program

Debugging is pausing the program to inspect what’s going on.
Just like when you ride a bike and you want to see where are your friends, so you decide on a “break point” to look behind you.
It’s possible to stop everything the program does by “Breaking” with a “Break point”.
Activate it by presssing on the panel of F9 on a line you want:

Now when you run using “F5” or the run button you’ll see it stops on this line:

This is something useful which will come more handy once you start writing more complex programs!

A small exercise

We’ll write code between the brackets after the Main definition { }
Try changing the “Hello World” into something different
For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;

namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Mars!");
Console.ReadLine();
}
}
}

Now you are all set up to write anything you want!
Copy paste thie code to play Guess the number game!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;

namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Welcome to guess the number!");

Random random = new Random();
const int maxTries = 10;
var currentTries = 0;
bool didWin = false;

while (true)
{
int randomNumber = random.Next(1, 100);
Console.WriteLine($"Guess a number between 1 and 100, You get {maxTries} tries!");

while (true)
{
Console.Write("Gues a number: ");
var line = Console.ReadLine();

var guessedNumber = 0;
var parseResult = int.TryParse(line, out guessedNumber);
if (!parseResult || (guessedNumber > 100 || guessedNumber < 1))
{
Console.WriteLine("Please input a valid number between 1 and 100");
continue;
}

if (guessedNumber == randomNumber)
{
didWin = true;
break;
}
else if (guessedNumber < randomNumber)
{
Console.WriteLine("The number is larger than this!");
}
else if (guessedNumber > randomNumber)
{
Console.WriteLine("The number is smaller than this!");
}

if (currentTries >= maxTries)
{
break;
}
++currentTries;
}


if (didWin)
{
Console.WriteLine($"You've guessed correctly {randomNumber} in {currentTries} tries.");
}
else
{
Console.WriteLine($"You've run out of tries! Better luck next time! The number was: {randomNumber}");
}

Console.WriteLine("Do you want to play again? Press Y to continue, anything else to exit.");

if (Console.ReadKey().Key == ConsoleKey.Y)
{
Console.Clear();
currentTries = 0;
continue;
}
else
{
break;
}
}

Console.WriteLine("Thanks for playing!");
}
}
}

Resources to continue and learn

A very simple tutorial about the basics can be found here:
https://www.javatpoint.com/c-sharp-tutorial

There is also this site:
https://www.learncs.org/

And of course Microsoft MSDN:
https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/

More:
https://channel9.msdn.com/Series/CSharp-101/?WT.mc_id=Educationalcsharp-c9-scottha

C# Challanges

What kind of difficulties one will have trying to learn C#?

  • Programming - if it’s your first programming language then you’ll definitly have challanges learning programming flows.
  • .Net framework API - for new comers and beginners .net is a vast framework with endless code to explore.
    This variety makes it longer to learn and use .net framework.
    Even experienced developers don’t know all the small details of the frameworks.
  • Memory handling - in the future where you will be the best developer you’ll need to learn how to manage complex software.
    This includes complex memory patterns and something that is called “Garbage collector”.
  • Meta-programming - It’s an advanced topic one will have to learn, some of it is not so complex and easy to understand but some topic are though like IL Emission.
  • Async-Await - It took me a while to learn it, it’s one of the most difficult topic to teach to new students.

Theory for the curious mind

What is C#?

C# is a “general purpose programming language”,
meaning it can do a lot of different jobs:

  • Video games
  • Business applications
  • Web sites
  • Mobile applications
  • VR games
  • Network servers

What is Visual Studio?

Visual studio is a program that helps us writing and building code.
It’s an - Integrated Development environment or in short “IDE”.
It gives us set of tools to help us produce code.
Like:

  • Debugging - Going step by step through code execution.
  • Building - Managing our porjects and code to produce programs.
  • Auto correct - If we misspell a function or a code name it will show us the error.
    and much more!

What is “Compilation”?

To “Compile” is the process of taking the code written in human language and turning into a code the machine understands.

What is DotNet or “.net”?

DotNet or “.net” is the framework which gives us “high level” classes to deal with.
We don’t need to create File handlers ourselves we can use the “File” class.

What is CLR?

The Common Language Runtime is the “Manager” which runs our code.
It handles errors, null checks, code execution, assemblies, memory, etc…
It what gives C# the ease of programming.

What is JIT?

I lied about the compilation - the CLR reads a special language which is called MSIL - Microsoft Intermediate language.
This is a special language C# is compiled to - and other dotnet languages like F# or VB.net.
Just in time is the compilation process between this MSIL and code that the computer understands.

This chart summarizes theme concepts:

Hope you’ve learned your share, and enjoy being a top-notch programmer!


Is this helpful? You can show your appreciation here: Buy me a coffee