Jump to content

The Ultimate C# Beginner's Guide

AlTech

Welcome to the Ultimate C# Beginner's Guide by @AluminiumTech and @jubjub.

 

Introductory note - In order to follow and do what we are doing in this guide you will need to download Visual Studio. We are using Visual Studio 2015 Community since it’s free. Go to https://go.microsoft.com/fwlink/?LinkId=532606&clcid=0x409 and then tick all the boxes shown in addition to whatever else you want to add.

 

post-89336-0-84269700-1447321808.png

 

post-89336-0-68846800-1447321817_thumb.p

 

NOTE - It should be noted that beginner's or anybody should comment on code using // or /// so that you remember what part does what. And also helps you in the learning process.

 

Before learning C#, or any programming language for that matter, you need to learn the “basics” of programming. In reality these “basics” are actually not all so basic. There is a lot of “jargon” that you need to learn and a lot of ideas that will seem strange. So let’s get into some of these ideas.

 

Note to Reader: Methods and functions are used interchangeably throughout the guide, while they are not exactly the same the difference will not matter at this point.

 

The first and probably most important idea in programming is around “data”. What is “data” in programming? Well data is simply what is stored in the computer, be it on hard drives/solid state drives or ram. In programming, you manipulate data through “objects”. These objects take many forms, whether as text which are called strings or as numbers which are called integers. Basically, when you need to use numbers you use an integer object and when you need text you will use the string object. These are all different data types. Each data type is handled differently when writing the code.

 

When writing code,you ask the computer to do something and you need to know in what order it will react. We achieve this by having something called a thread, this is something that  a lot of programmers still struggle with. However, it can be simplified. Basically when reading lines of code grouped together you see an order. This is the order that this code will run. When you see the program you have a main group of code inside a method that gets run first (more about methods later, just think of this as a group of code). We can have multiple blocks by having multiple methods.

 

Now we can talk more about the groups of code. These groups of code are called blocks, they are anything that gets run together sequentially. These blocks are what make up methods.

 

(I can write the intro to programming and basic concepts so if you could do some basic syntax introductions with examples that would be helpful. Also make sure to go backwards and start at one line with the print method and go backwards explaining each feature of the language. Even explain classes and functions/methods but be brief and use simple words without going in depth. I think classes will require a whole paragraph to explain.)

 

There are many different types of programs you can create using C# and the .NET library. These are:

  • Applications that interface with a console and text.

  • Applications that interface with a graphical user interface (GUI). This allows you to create buttons from code and such.

  • Applications that have a GUI which is created using a markup language outside of C#.

  • Universal applications which can scale across Windows Tablets, PCs, Windows Phones and Xbox Ones

 

Classes

Classes allow you to produce a desired outcome by combining and grouping variables, methods or events. You can think of classes as a blueprint.

 

Functions

A function is something that is called when you want to perform a certain task. So think of it as calling a mechanic to repair your car. You need to call the mechanic in order for him to come and fix it. In this analogy the actual fixing of your car would be the code inside the function.The same goes with programming.

 

using System.x

In C# we use the .NET framework to enhance functionality of programs. By default there is a set of mini frameworks that the program will contain. When you add it, if it is ever used it will appear in black font and if it is not ever used it is it will appear in gray font. Note - We added the System.Threading; mini framework and we’ll  explain that later on in this guide.

 

Ways of storing data

In programming in general, there are many ways we can use to store data. Some examples include strings, doubles, ints and floats, Strings are used to store text. Ints are used to store integers, otherwise known as whole numbers and they are 32 bit numbers. Doubles are used to store very large whole numbers, 64 bit numbers to be precise. The largest 32 Bit number/value is  2,147,483,647 (2.1 Billion)! Whereas the maximum 64 bit number/value is 9,223,372,036,854,775,807 (9.2 Quintillion)!. Floats are 32 bit numbers/values with numbers after the decimal point (e.g 1.4893). There are some positives and negatives to using floats, ints and doubles which I’ve summarised in the table below.

 
post-89336-0-54858700-1447322028.png
 

The reason why Doubles use more RAM is because the data stored, whether it be Strings, ints, floats or doubles, needs to be stored in RAM and the larger the size of data, the more RAM it will take up. What we are doing here is making a trade-off, if you remember from high school physics you can trade-off different work loads, e.g a lever. What we are trading is RAM for CPU, as the other option would be to have the CPU work trying to figure out whether it needs more ram or less ram. Some programming languages like Lua do this.


What is Source Code

Lines of code which make up a program.

 

What is an IDE

An IDE (or Integrated Development Environment) is usually a place where programmers write, test and debug code. It contains a place to edit the code as well edit the program and has options to export said code. Different IDEs work with different programming languages and as such you need to pick one for your programming language. For C++ and C#, we recommend using Visual Studio Community (Which is the free version of Visual Studio) 2015. For Java we recommend using Eclipse or Netbeans and for HTML, PHP and javascript we recommend Komodo Edit or Adobe Dreamweaver.


What is a compiler

A compiler is a program that translates your source code (see above) into a set of very basic instructions called Assembly Code. Think of it as a translator between you and your computer, except the translator doesn’t know english it knows C# (or whatever language you are using).

 

 

Starting to code

Now that you understand that, we’re going to move onto writing the first lines of code in a console with text.


In C# we use Console.WriteLine to print something to the console using text, this is a very easy way to display text.To have it print Hello World we can do the following.

 

post-89336-0-16841200-1447322205.png

 

Now let’s add a title to it. To do this we use Console.Title . Since we’re doing the beginner’s guide let’s go put C# Beginner’s guide as a title, like so.

 

post-89336-0-00011500-1447322304.png

 

Next we’re going to take things a step further by adding strings. In this case we have to create the string before we can use it. So let’s create a string called Title and a another string called Text.

 

post-89336-0-16748700-1447322407.png

 

Now it’s time to make this look a little bit better. We’re going to change the font colour by using Console.BackgroundColor. For this example we’ll make the text white.

 

post-89336-0-23820000-1447322450.png

 

Next we’ll move onto applications with a Graphical User Interface. The process for creating these type of applications are very different compared to command line text based programs. This is due to being much more advanced and the structure of an application being more complex, which can lead to many business and professionals using applications with a GUI instead of command line. In C# we call applications with a GUI “Windows Forms applications/programs.

 

First we need to create a new one in visual studio. When doing this you must uncheck the “Add to source control” and “Create directory for solution”. If enabled, this creates a small part of your project in your computer somewhere and if you try to use it on another computer ever again then it won’t work and you won’t be able to publish it.

Now that we’ve created the project let’s do some miscellaneous stuff. First we’re going to  change the name of the form to “C# Beginner’s Guide Windows Form”. You do this by going to text in the right hand corner and type in the name of your choice into the the “Text” field.

 

post-89336-0-32933200-1447322785_thumb.p

 

Now that we’ve done that we can make the Form larger to accommodate our needs. You do this by dragging the bottom corner outwards.

 

post-89336-0-84131100-1447322830.png

 

At this stage, the programmer or creator decides what he/she wants the program to do. For this demo, we’ll show you the fundamentals of how C# works by creating an application which responds to user input. User input is any interaction a user has with a mouse or a keyboard with a program. In this program we’re going to tell the user when they click a button. We’ll do this by using a richtextbox. But first we need to get it from the “toolbox”. If you can’t see it or find it then go to “View” and then a bit under that there should be “toolbox”.

 

post-89336-0-07670300-1447322954.png

 

Once you’re done dragging it onto our Form, you will also need to drag a button to the Form. For the sake of neatness we went with putting our button centered at the bottom.

 

post-89336-0-39640900-1447323015.png

 

We’re now going to rename our button so we can easily refer to it in our code. To do this, click once on the button and move your cursor to the properties tab on the right and scroll until you find the “Design” section and you find design name. Visual Studio can be quite picky about the design names so we’ll just stick with “button”. It’s important to note that spaces will not work on this and instead they will require “_”.  We’re also going to name the richtextbox to “text”

 

post-89336-0-93917000-1447323057_thumb.p

 

Remember how we earlier talked about C# being event based? It still applies here. First we’re gonna make the richtextbox output some text once the button is pressed. We can do this by pressing the events icon in the below screenshot.

 

post-89336-0-85454100-1447323122_thumb.p

 

To create an event handler we need to double click on the appropriate event and double click.

Now Visual Studio is generating some code for us when we use event handlers. All we need is the code which tells the richtextbox to display text. We do this by inputting the code shown in the screenshot below.

 

post-89336-0-70183400-1447323076_thumb.p

 

The /r/n will mean that after it creates the text it will make it go down one line and will make space for another piece of text should we want one. Now we’re going to take a look at functions and how they work. In this example we’ll create a function called “WelcomeMessage”. We can do that like so…

 

post-89336-0-90610000-1447323400.png

 

That's the end of the code for the guide.

 

Functions/Methods:

In C# in order to get the computer to run something you need functions. These are a statement explaining what you want the function to be like followed by an opening “{“ and closing “}”. Everything inside those fancy brackets is code that gets run when the function gets called.

 

To create a function, you need to first declare the type (private or public - this doesn't matter too much so don't worry about it too much, if you're unsure then pick private). After that, we leave a space and type "void". This next bit can be a little annoying if you get it wrong so pay attention and take notes. We have to obey the same rules as we did for the design name of the form. We can't use spaces, or anything other than letters and underscores. We then need to add a double special bracket or {} when first creating it. Press enter and open the insides of the bracket so that you can put code inside the function. For example Welcome_Message{

 

 

}

When calling the function it needs to be exactly the name you called it above but we need to add double regular brackets instead and a semi colon. For example

 

Welcome_Message();

 

I know this didn’t cover too much but we have covered basic principles, ideas, items and even some code.


This was just a beginner’s guide! if you‘re interested then feel free to leave a comment letting me know if you want a guide on how to make useful programs in C#. And how to make more complicated Windows Forms programs.

 

Wow, did this take ages to complete.

 

Thank you very much @jubjub for helping me out on this! I really appreciate it!

 

I am in the process of creating an Interactive edition and will update the post when that's complete.

 

I'm AluminiumTech and until next time, Happy Programming :)

 

EDIT - Also made a PDF of this

 

Ultimate C sharp beginner guide.pdf

post-89336-0-79448000-1447323249.png

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

That's a long ass guide. Nice one.

My Gaming Rig;  Motherboard - ASUS Maximus VI Hero | CPU - Intel i5 4670k @4.5Ghz 1.25v | GPU - GIGABYTE GTX 980 @Stock | RAM -  16GB Corsair Vengeance @1866Mhz | CPU Cooler - Corsair H100i | Storage #1 - Samsung 840 Basic 250GB SSD | Storage #2 - Sandisk II 480GB SSD | Storage #3 - 2TB 7200rpm 64mb HDDPSU - Corsair HX750 | Case - Fractal Design R4 |

Link to comment
Share on other sites

Link to post
Share on other sites

That's a long ass guide. Nice one.

Thanks! Just realised that it might be too long for one sitting. so I've also made a PDF version and I'm adding it now.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks! Just realised that it might be too long for one sitting. so I've also made a PDF version and I'm adding it now.

It's a good guide that will hopefully allow some people to start to learn c#.

Main Rig - Case: Corsair 200R   Motherboard: Gigabyte GA-Z270-GAMING-K3  CPU: Intel i5 7600 RAM: Corsair H55 RAM: Corsair Vengeance 16GB 3000MHz SSD: Crucial MX500 1 TB 

HDD: 2TB WD Green  GPU: Gigabyte GTX 1660 Ti 6GB Windforce  PSU: Corsair CX 600W  

HTPC - Case: CiT MTX-007B   Motherboard: Biostar H61MGV3, CPU: Intel i5 2400  RAM: Patriot 4GB 1333MHz SSD: 240GB Toshiba SSD PSU: 180W CIT (Came with case)

Corsair 200R Front Bezel Mod

Link to comment
Share on other sites

Link to post
Share on other sites

It's a good guide that will hopefully allow some people to start to learn c#.

Thanks a lot!  :) .

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks a lot!  :) .

I would also advise any beginners to add comments to any code they write. Using '//' Allows for comments that will have no effect on the code but will benefit you in case you want to modify the code a year or 2 later or if someone else wants to modify it. 

Main Rig - Case: Corsair 200R   Motherboard: Gigabyte GA-Z270-GAMING-K3  CPU: Intel i5 7600 RAM: Corsair H55 RAM: Corsair Vengeance 16GB 3000MHz SSD: Crucial MX500 1 TB 

HDD: 2TB WD Green  GPU: Gigabyte GTX 1660 Ti 6GB Windforce  PSU: Corsair CX 600W  

HTPC - Case: CiT MTX-007B   Motherboard: Biostar H61MGV3, CPU: Intel i5 2400  RAM: Patriot 4GB 1333MHz SSD: 240GB Toshiba SSD PSU: 180W CIT (Came with case)

Corsair 200R Front Bezel Mod

Link to comment
Share on other sites

Link to post
Share on other sites

Will you be making more guides? :) This would've helped me a lot when I started C#.

Probably. I may make guides about some more complicated programs and maybe even do a beginner's guide to java. Throughout the creation of this, i felt that it would have helped a lot with me learning C#. I initially watched youtube tutorials, analyzed them, found which part of the code does what and through practice I became a lot more confident.

 

 

I would also advise any beginners to add comments to any code they write. Using '//' Allows for comments that will have no effect on the code but will benefit you in case you want to modify the code a year or 2 later or if someone else wants to modify it. 

Haha. The one thing I forget lol. It's cos I don't comment code that much anymore but I did a lot when I started learning it. I'm gonna try and get back into the habit of commenting.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Probably. I may make guides about some more complicated programs and maybe even do a beginner's guide to java. Throughout the creation of this, i felt that it would have helped a lot with me learning C#. I initially watched youtube tutorials, analyzed them, found which part of the code does what and through practice I became a lot more confident.

 

 

Haha. The one thing I forget lol. It's cos I don't comment code that much anymore but I did a lot when I started learning it. I'm gonna try and get back into the habit of commenting.

I always do comments when im writing code. its the only way i can remember what bit of code does what.

Main Rig - Case: Corsair 200R   Motherboard: Gigabyte GA-Z270-GAMING-K3  CPU: Intel i5 7600 RAM: Corsair H55 RAM: Corsair Vengeance 16GB 3000MHz SSD: Crucial MX500 1 TB 

HDD: 2TB WD Green  GPU: Gigabyte GTX 1660 Ti 6GB Windforce  PSU: Corsair CX 600W  

HTPC - Case: CiT MTX-007B   Motherboard: Biostar H61MGV3, CPU: Intel i5 2400  RAM: Patriot 4GB 1333MHz SSD: 240GB Toshiba SSD PSU: 180W CIT (Came with case)

Corsair 200R Front Bezel Mod

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you, Thank you Thank you. Being 35 and learning code is really really hard for me. I know my way around a computer and can do most PC tasks, but I will tell you this:

I will never ever criticize a program Dev again....or it least until I get good at programming.

Thank you for being apart of Team Human :)

Link to comment
Share on other sites

Link to post
Share on other sites

To be used in conjunction with this guide, I highly recommend Barnacules Nerdgasm's Codegasm series on YouTube. 

 

https://www.youtube.com/playlist?list=PLEbaEyM-xt9mVQEAXGlRRmbO2Qp_oqF-n

CPU- i5 4670K CPU Cooler- Corsair H90 MB- MSI Z87 GD-45 RAM- 16gb G.Skill Ripjaws Case- NZXT Phantom 530

PSU- Corsair CX750M Cooler- Corsair H90 Boot Drive- Samsung 850 Evo 250GB

Storage-WD 1TB Blue Storage- Seagate 2TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×