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 | using System; |
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 | using System; |
Now you are all set up to write anything you want!
Copy paste thie code to play Guess the number game!
1 | using System; |
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!