Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C# - Entry level Programming - 1

#1
Welcome to the first entry of my C# programming guides. Through this tutorial I hope to provide you with enough knowledge to begin your adventure with the C# Programming language. I will be going over various entry-level programming topics through this thread and hope they will assist you with either learning the language or migrate to it from another such as VB, java or even C++.

Due to the amount of content I wish to cover over the duration of this series, I will be splitting the tutorials into various threads based on content. Once more than one entry to the series has been released I will create a "Master Index" thread with links to all entries in the tutorial.

Table of contents
  1. Introduction to .NET
  2. Getting Started - Visual Studio
  3. Hello World
1. Introduction
The C# programming language is a "high level" programming language built on the .NET framework; a platform provided by Microsoft to assist with rapid application development for Microsoft based platforms. With this being said it is still possible to use the C# programming language to develop for alternative platforms such as UNIX based OS and MAC OS X via the mono framework.

Over the years Microsoft (hence forth will be MS), has released various updates to the framework correcting bugs, adding features and updating features. For the purposes of these tutorials I will be providing code relevant for .NET 4.5.2. Please note that this means some of the code will not be compatible with newer and or older versions of the framework.

So what is this framework?

Think of the .NET framework as a base for your programs. The framework provides us with numerous built-in features to assist with developing software for Windows base OS. In other words it's going to provide you with a quick way to complete tasks that may not be so quick in other languages. The .NET framework also provides us with other neat and nifty tools such as the JIT debugger, and CLR.

JIT; Just in time debugger allows provides an easy way for us to debug our code on the fly.
CLR; Common Language Run-time allows our code to be executed on any system with the correct version of the .NET framework.

However, when using the .NET frame and programming in C# there is a certain pipeline in which you program will follow in order to execute in which makes it different from a native language such a C++.

The section in the spoiler is a little advanced for a beginner. You may attempt to understand it if you'd like or skip-over it and come back to it at another time. It's not important per say however, it does briefly describe how your applications will compile.
References:
https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx (C# Programming Guide)
http://www.mono-project.com (Mono framework)
https://msdn.microsoft.com/en-us/library/bb822049(v=vs.110).aspx (.NET Framework Version Guide)

2. Getting Started - Visual Studio
Before we can jump into coding and learning how to use C# to make our life's easier we must first setup our ide; integrated development environment of choice. In this tutorial series we will be using Visual Studio as it provides us with many useful features for debugging and compiling our code.

There are different versions of Visual Studio available for you depending on your needs however, assuming we are all personal hobbyist or education oriented the community edition will suit all of our needs just fine. You can access the latest version of the community edition directly on the MS website via the link below:
https://www.visualstudio.com/downloads/

WARNING PLEASE ONLY EVER INSTALL VISUAL STUDIO FROM OFFICE MS WEBSITE OTHER SOURCES MAY CONTAIN VIRUS

Once you have downloaded the VS(Visual Studio) installer you will be prompt to select the tools in which you wish to install. You will want to select the Windows development tools sections as well as any Azure sections if you are looking to get into ASP.NET development at some point. Don't worry if you miss something or want to install more later, you can always upgrade.

[Image: e9104a8b02cc63d64baee56c029caea2.png]

3. Hello World
Now that Visual studio has installed you should be ready to begin your software development career. To make sure everything is setup correctly let's create a simple "Hello World" application.

To begin, open Visual Studio 2017. This may take a few moments if this is the first time you've ever ran Visual Studio as it will need to setup the environment for you. Don't worry if it seems like it's not responding it's just busy creating files and setting defaults.

Once loaded, along the menu bar at the top please click "File->New->Project" or "Ctrl+Shift+" on your keyboard. This will open the new project dialog in which will provide you with various options depending on the .NET components you installed.

For the purpose of this tutorial you will want to find the "Console App (.NET Framework)" template under the "Visual C#" section.

It should look something like this:
[Image: 5a53a5038d4f9c156ec9e309b15a324b.png]

You may name it anything you want. I'll be naming mine "Hello World" and storing it under a folders on my D: drive. You can change where the project files are created and stored via the "Browse" button.

Once you are ready simply click "OK" and Visual Studio will begin setting up a new project for you.

At this point you should be brought to a new tab in Visual Studio in which you should see a bunch of code automagically generated for you. Since this is only a hello world I've posted the code for this project on pastebin.

What we want our program to do is simply say "Hello World" on the screen.
[img]https://i.gyazo.com/172326ff02c35fb75b1cfd5b1c1b5a78.png[/img]

For this to happen a few things need to be done.


  1. When the program is opened the operating system needs to find code to run -> Main entry-point
  2. The code needs to somehow output a message onto the screen for the user -> Command Prompt Window or Console Window in this case
  3. Once the message is output, we would ideally like the window to stay open so we can read it -> Pause the program from closing.

Something to keep in mind is that once your program has finished executing all of it's code it will close by itself as there is nothing left to do. Thus in our program we are going to stop this from happening so that we may read the message on the screen.



Back to the list...


The first thing we need to worry about is where our program is going to start. This is called the "Main Entry-point" of your program. Any executable program has one and when you open a program your OS; operating system will look for it and execute it. Thus, this peice of code is one of the most important sections of your program as it's purpose is to setup the program or environment for your program to run in. Since our program is simple all we need it to do is output a message and wait for code to come back.



By default, Visual Studio will generate this entry-point for you and call it static void Main(string[] args)



Anything in the following "Curly Brackets"; { } is considered part of the main entry-point block of code. This is where our code will need to go.



Between the curly braces please create a new line and enter the following code:


Code:
Console.WriteLine("Hello world");

So all together you program should look something like this within the entry-point:

PHP Code:
static void Main(string[] args)

      {
          Console.WriteLine("Hello World"); 
      

What I want you to do now is click the Green Start Button at the top of visual studio. This will compile and run your program.

What should happen next is the program will open quickly, output the message and close right away. This is because there is nothing left for our program to do once we tell it to output stuff to the console window. To keep it open in order to see the message we need to pause the program.

There are numerous ways to do this however, the easiest way to do this is to tell the program to wait for user input. As you can see before we said "Console.WriteLine". This means we are telling our program to find the Console window linked to it and write a line of text on the screen. We can also tell this window to read input from a user so that it doesn't close right away.

To do this, on a new line enter:

PHP Code:
Console.ReadKey(); 

This tells our program to stop and wait for the user to press a key on their keyboard before continuing.

Now press the play button again and if you did everything correctly the program should open and output "Hello world" on the screen and stay open until you press a button on your keyboard.

I forgot to mention that you need to end each statement of your code with a semi-colon ';'. This tells the compiler that that line of code is done and anything after it is a separate statement of code. I will go more into this in the next tutorial when we begin C# data types and basic flow of your programs.

This concludes this tutorial, I've uploaded the source of the hello world program to pastebin with additional comments in it. Please take a look at it before continuing the tutorial series.

https://pastebin.com/cwzibcys

If you have any questions, concerns or suggestions please leave a comment below.
Reply

#2
Cool tutorial, I like that you included how to get the software. That was one huge annoyance for me when I was trying to learn programming. They were all tutorials on code and I had no idea how to run it.

(04-11-2017, 06:05 AM)HookJordan Wrote: I forgot to mention that you need to end each line of your code with a semi-colon ';'. This tells the compiler that that line of code is done and anything after it is a separate statement of code. I will go more into this in the next tutorial when we begin C# data types and basic flow of your programs.

This might confuse people however, not every line ends with a semi-colon. 
Fool I'mma vet you can bet That I can dance underwater and not get wet






Reply

#3
(04-11-2017, 11:57 AM)Marsh Wrote: Cool tutorial, I like that you included how to get the software. That was one huge annoyance for me when I was trying to learn programming. They were all tutorials on code and I had no idea how to run it.

(04-11-2017, 06:05 AM)HookJordan Wrote: I forgot to mention that you need to end each line of your code with a semi-colon ';'. This tells the compiler that that line of code is done and anything after it is a separate statement of code. I will go more into this in the next tutorial when we begin C# data types and basic flow of your programs.

This might confuse people however, not every line ends with a semi-colon. 

This is true.

I've revised the sentence to say "each statement", or should I just remove that line altogether and save it for the next tutorial which will be datatypes.
Reply

#4
Like to see this kind of contribution! Keep em coming!
Reply

#5
I would just say most lines of code require a ; but there are a few exceptions like class or method declarations that we will go over later or something similar...
Reply

#6
Sorry for long delay before the second entry to this series comes out. I've recently started a co-op position and just having a little bit of difficulty finding time to work on it at the moment. Fear not however, as I will eventually finish it and continue working on the rest.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB 1.8.4 Theme © iAndrew 2016