C# Course for Absolute Beginners – Lesson 1.2
In this lab we’re going to build our first example using Visual C Sharp Express Edition. If you’re new to programming, almost every tutorial starts off with what’s called the "Hello World" example. Follow along and do exactly what this lab does. We’re going to use this tiny example as the basis for several additional discussions in future labs. We’ll dissect every single part about it.
BONUS: Watch the Video for this Lesson!
Step 1: Create a new project
Start by creating a new project.
(1) From the start page click "New Project"
(2) On the following page select a C# console application
(3) Change the name to "Day One" and then click okay.
Step 2: Create your first line of code
(1) Underneath this curly brace near the static void main, open and close curly brace, I’m going to type a line of code:
(2) Select the triangle button that says ‘Start Debugging,’
You’re going to see your application run very quickly. A console application displays the Windows console, or rather as some like to think about it, the old DOS prompt. So to start off we’re going to go old school. Don’t worry, we’re only starting here so we’re not encumbered by displaying windows and such. We want to keep it simple and talk about the C# language first.
Remember to do everything that I did. Follow exactly, re-read the lab, do whatever you have to do in order to get that same result. You may say, ‘what result? That didn’t look like much!’ That’s true. That’s because our application ran very briefly and then it stopped. We didn’t even get a chance to see what was on the screen, but we will soon.
Step 3: Add the Console.ReadLine(); code
(1) Now add one more line of code:
(2) Select that same button again, ‘Start Debugging.’ Or you could also go up to the debug menu and select ‘start debugging.’ So you can access it from several places.
Step 4: View your first program results
This is a Windows console window. You’ll see a black background with the white text, unless you made some changes to those options. You’ll see our message, ‘Hello World’ with a little dash and cursor underneath of it. If we hit any key on our keyboard, our application will exit.
Step 5: Understanding the files in our program
So what does this all mean? What did we do, exactly? First of all, we created a new project. We can see the files that belong to the project within our IDE. The IDE stands for interactive development environment. It’s the program, in this case Visual C# Express Edition, where you type all your code, where you manage all the files that belong to, or get compiled into, an entire application.
(1) This little window shows us the files, particularly the Program.cs file that we then see in this main area here within the IDE. The Program.cs file is where we typed in our code. We’ll come back to this properties window later.
(2) We’ll also talk later about this little thing called the toolbox.
(3) You can see there are a number of icons on the toolbar, most notably the Save and Open, and things of that nature. We already used the start debugging button. So, already we’re starting to get just mildly familiar with the user interface of the Express Edition tool.
Step 6: The importance of code placement
Let’s discuss the composition of the application.
(1) Notice that you wrote the code between these curly braces. If you didn’t do that then you probably got some very unpredictable results. You may have even seen an error message.
Step 7: Intentionally creating an error message
To see an example error message, highlight the code and select the Control-X on your keyboard.
(1) Move your mouse cursor down underneath that area and paste the code in here instead
(2) You can see all of a sudden some red squiggly lines. If you hover your mouse over them you’ll see a pop-up that says “system console write line is a method but it’s used like a type”
Step 8: Finding detailed information about the error message
(1) If you were to try to run it you would get a message that said “there were build errors, would like to continue?” (Select No).
(2) You’ll see this error list pop up.
Remember: It’s important that you follow along, at least at first until you get more familiar with the process. Essentially what we did was we wrote some code in a very special method (we’ll talk about methods later) in a very special area of this application called Main. And Main, by default, will be what is executed first whenever our application is run and the code between the two curly braces (this is called a code block) is what will get executed whenever our application runs.
Step 9: Saving and closing the project
Let’s do, this just for fun:
Close the application; close the solution down
It will ask "do you want to discard the changes", no – save those. Save this into the default location, which will put it in your documents directory on Window 7. It will also create this directory for the solution called Day1. We’ll talk about solutions and projects and their relationship a little bit later. For now just click save and shut down Visual C#.
Step 10: Finding the files in our computer
(1) Now we’ll go to our documents directory in Windows Explorer just to see where everything’s at.
(2) Double-click on the Visual Studio folder and then double-click on the Projects folder; you can see the Day1 solution folder, and in that there is what’s called a solution file. You can see under the description ‘Microsoft Visual Studio solution’
Step 11: Locating the program.cs file
If you go into the Day1 folder you can see that there’s another Day1 file but this time it’s a Visual C# project file. Again, let’s come back to solutions and projects a little bit later. Here’s the program.cs file that we saw earlier, and then if we look in the bin directory we would see two folders called Debug and Release.
Step 12: Locating the executable file
If we go into the Debug folder you can see that we have something here called Day1. I have the extensions hidden by default in Windows 7, but it’s Day1.exe. EXE should trigger your mind thinking that this is an executable file. So this is where, at least in the Debug version, (we’ll talk about the difference between debug and release versions a little bit later) the EXE file was created whenever we hit ‘start debugging’ on our application.
Step 13: Opening the existing project
What if we wanted to load our program back up, and work on it a little bit more? There’s a couple of ways we could go about it.
-We could simply double-click the solution file (the Microsoft Visual Studio solution)
-We could double-click the project file.
-You can open Visual C# Express Edition back up, click file, open project, and you’ll see the open Project dialog. You can drill in and find the project you’re looking for.
Step 14: Visual Studio IDE Start Page overview
Here’s another idea:
(1) You can go to the ‘Recent Projects’ section of the start page which pops up. The start page is just an area that’s kind of a jump-off for both your own projects and the community.
(2) You can see that Microsoft’s posted the “Beginning Developer Learning Center,” where some of our videos are posted.
(3) Also the “Coding for Fun” site where you can see some examples of things that people built with Legos in .NET and things of that nature,
There are also some other additional tutorials and also news from an RSS feed. You can discover those things on your own.
Step 15: The pin option
Go ahead and click recent projects. You’ll see if you hover over it it has a little pin. As you build more projects this list grows longer and longer, some projects will get pushed off and won’t be able to be seen anymore on this list. So you can pin this down, and no matter how many projects you create, this Day1 project will always pop up at the top of the list.
Step 16: Returning to our project
Click the link and you’re back to the application. So that’s a good starting point, we can build on this. We’ll talk more about the structure of C# applications, the structure of console applications, and what these code blocks mean. We’re going to add more C# language elements. We’ll talk about this console.writeline with the open parenthesis. We’ll talk about all that in the next lab. So if this doesn’t work for you, please make sure you get at least this far before you continue on so that you don’t become completely lost.
In this lab, we created our first C# application. We learned about error messages, and how to save our application. We learned how to close the Visual Studio IDE, where the files are saved on our computer, and how to open our project again.