- How to create new projects
- What the Solution Explorer is
- The various files that make up of a C# .NET project
- How to save your work
- How to run programmes
- The importance of the Main statement
Open the Visual C# Express software from your programs menu. When you first open C# (pronounced C Sharp), you should see a screen something like this (screenshot is for 2008 version, but 2005 version is the same):
If you have the 2010 version of Visual C# Express then your screen looks like this:
When you're looking at this piece of software for the first time, it can seem hugely complex and daunting. The temptation is to think you're never going to get to grips with something so difficult. But don't worry - after a few lessons things will start to feel familiar, and you won't feel nearly half as intimidated as you do now!
A Simple C# Console Application
A Console Application is one that looks like a DOS window. If you don't know what these are, click your Start menu in the bottom left of your screen. Click on Run. From the dialogue box that appears, type cmd:Click OK and you'll see a black screen, like this one:
So with Visual C# Express open, click File from the menu bar at the top. From the File menu, select New Project:
When you click OK, a new Console Application project will be created for you. Some code should be displayed:
As well as the code, have a look on the right hand side and you'll see the Solution Explorer. This is where all the files for your project are. (If you can't see the Solution Explorer, click View from the C# menu bar at the top. From the View menu, click Solution Explorer.)
The code itself will look very complicated, if you're new to programming. We'll get to it shortly. For now, right click the Program.cs tab at the top, and click Close from the menu that appears:
Now click the plus symbol next to Properties in the Solution Explorer above. You'll see the following:
But right click the AssemblyInfo.cs tab at the top, and click Close from the menu. Now, in the Solution Explorer, click the plus symbol next to References:
Before we add some code, let's save the project. We'll do that in the next part below.
Saving your work in C# .NET
When you save your work, C# will create quite a few folders and files for you. Click File from the menu bar at the top of the Visual C# Express software, then Save All:
C:\Users\Owner\documents\visual studio 2010\Projects
In XP, however, you'll see something like this:
C:\Documents and Settings\kayspc\My Documents\Visual Studio 2008\Projects
So it's going to be saved to the "documents" folder of this computer. In the "documents" folder you'll find another one called Visual Studio 2010. In this folder there will be one called Projects.Before clicking the Save button, make sure there is a tick in the box for "Create directory for solution". Then click Save.
Now open up Windows Explorer (Hold down the Windows key on your keyboard, then press the letter "e"). Navigate to the folder location above. In the image below, we've used Windows Explorer to navigate to the Visual Studio 2010 folder:
Double click your ConsoleApplication1 folder, though, to see inside of it:
Your first line of C# code
The only thing we'll do with the code is to write some text to the screen. But here's the code that Visual C# prepares for you when you first create a Console Application:The thing that's important above is the word class. All your code will be written in classes. This one is called Program (you can call them anything you like, as long as C# hasn't taken the word for itself). But think of a class as a segment of code that you give a name to.
Inside of the class called Program there is this code:
static void Main(string[] args)
{
}
This piece of code is something called a Method. The name of the Method above is Main. When you run your programme, C# looks for a Method called Main. It uses the Main Method as the starting point for your programmes. It then executes any code between those two curly brackets. The blue words above are all special words - keywords. You'll learn more about them in later chapters.{
}
But position your cursor after the first curly bracket, and then hit the enter key on your keyboard:
The single line of code we'll write is this (but don't write it yet):
Console.WriteLine("Hello C Sharp!");
First, type the letter "C". You'll see a popup menu. This popup menu is called the IntelliSense menu. It tries to guess what you want, and allows you to quickly add the item from the list. But it should look like this, after you have typed a capital letter "C":
C# 2010
Older versions of C#
"Hello C Sharp!"
Don't forget the double quotes at the start and end. These tell C# that you want text. Your code will look like this:The thing we've missed out is a semicolon. All complete lines of code in C# must end with a semicolon. Miss one out and you'll get error messages. Type the semicolon at the end and the red wiggly line will go away. Your code should now look like this:
(If you want, you can change these colours. From the menu bar at the top, click Tools > Options. Under Environment, click Fonts and Colors.)
Time now to Build and Run your code!
How to Run your C# Programmes
You can test your programme a number of ways. First, though, it has to be built. This is when everything is checked to see if there are any errors. Try this:- From the View menu at the top of Visual C# Express, click Output. You’ll see a window appear at the bottom. (In C# 2010, if you can't see an Output entry, click the Tools menu. From the Tools menu, select Settings > Expert Settings. The Output menu item should then appear on the View menu.)
- From the Build menu at the top of Visual C# Express, click Build Solution
- You should see the following report:
Build: 1 succeeded or up-to-date, 0 failed, 0 skipped
That's telling you that everything is OK.Now try this:
- Delete the semicolon from the end of your line of code
- Click Build > Build Solution again
- Examine the output window
Compile complete -- 1 errors, 0 warnings
Build: 0 succeeded or up-to-date, 1 failed, 0 skipped
So it's telling you that it couldn't build your solution because there was 1 error.
Put the semicolon back at the end of the line. Now click Debug from the menu at the top of Visual C# Express. From the Debug menu, select Start Debugging.
You should see a black DOS window appear and then disappear. Your programme has run successfully!
To actually see your line of text, click Debug > Start Without Debugging. You should now see this:
No comments:
Post a Comment