Apr 26, 2011

Started with C# .NET

What we're going to do first is to create a very simple programme, so that you can see what makes up a C# .NET project. By the end of this chapter, you'll have learnt the following:
  • 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
The simple programme we'll create is called a Console Application. We won't be doing much else with this type of application, as this is a book about Windows Applications. Off we go then!
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:
The Run Box in Windows XP
In Vista and Windows 7, type cmd in the search box at the bottom of the start menu. You'll then see the search results appear:
CMD search in Windows 7
Click cmd.exe to see the console appear.
Click OK and you'll see a black screen, like this one:
A DOS Window
This is the type of window you'll see for our Console Application. When you create your Windows forms, there's a whole lot of code to get used to. But Console Applications start off fairly simple, and you can see which part of the programme is the most important.
So with Visual C# Express open, click File from the menu bar at the top. From the File menu, select New Project:
New Project menu in C# 2010
When you click on New Project, you'll see the following dialogue box appear:
The New Project dialogue box in C# Express
Or this one in version 2010 of the software:
New Console application in C# 2010
This is where you select the type of project you want to create. If you only have the Express edition of Visual C#, the options are limited. For the rest of this book, we'll be creating Windows Applications. For now, select Console Application. Then click OK.
When you click OK, a new Console Application project will be created for you. Some code should be displayed:
The Coding Window in Visual C# Express
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:
Close the Program
Or just click the X in C# 2010 :
Close Program
Now double click the Program.cs file in the Solution Explorer:
The Solution Explorer in C# Express
When you double click Program.cs, you should see the code reappear. So this code is the programme that will run when anyone starts your application.
Now click the plus symbol next to Properties in the Solution Explorer above. You'll see the following:
The Properties option in the Solution Explorer
The file called AssemblyInfo.cs contains information about your programme. Double click this file to open it up and see the code. Here's just some of it:
AssemblyInfo.cs
The reddish colour text is something you can change. You can add a Title, Description, Copyright, Trademark, etc.
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:
Console Application references
These are references to code built in to C# (you won't see as many entries in earlier versions of the software). Much later, you'll see how to add your own files to this section.
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:
File,  Save All menu
When you click Save All, you'll see the following dialogue box appear:
The Save Project dialogue box in C# .NET
You can type any name you like for your project. The default Name is ConsoleApplication1. Have a look at the location of the project, though:
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:
Folders in the Documents library
Double click the Projects folder to see inside of it. Your should see a folder called ConsoleApplication1. Double click this folder and you'll see the following:
Folder View
So there's another folder called ConsoleApplication1. There's also two files: one that ends in sln, and one that ends in suo. The sln file is the entire solution. Have a look at the Solution Explorer again:
Solution Explorer
The one highlighted in blue at the top refers to the sln file. The suo file contains information about the Visual Studio environment - whether the plus symbols are expanded in the Solution Explorer, what other files you have open in this project, and a whole host of other settings. (If you can't see the suo file click Tools > Folder Option in Windows Explorer. In Vista and Windows 7, you may have to click Organise > Layout > Menu Bar first. Click the View tab, and select the option for "Show hidden files and folders".)
Double click your ConsoleApplication1 folder, though, to see inside of it:
Folders and files in the Console Application
Now we have three more folders and two files. You can see the bin and obj folders in the Solution Explorer:
Show all Files
Click ConsoleApplication1, second from the top. Then click the icon for Show all Files, circled in red in the image above. The bin and obj folders will appear. Click the plus arrows to see what's inside of these folders:
The bin and obj folders
The important one for us is the Debug folder under bin (there'll be an extra file ending in .manifest in c# 2010). You'll see why it's important in a moment. However, it's time to write some code!

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:
Visual C# code
For now, ignore the 3 lines that start with using (we'll get to them later in the book). But they add references to in-built code. The namespace line includes the name of your application. A namespace is a way to group related code together. Again, don't worry about the term namespace, as you'll learn about these later.
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 C# Main code
The cursor automatically indents for you, ready to type something. Note where the curly brackets are, though, in the code above. You have a pair for class Program, and a pair for the Main method. Miss one out and you'll get error messages.
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
Intellisense list in C# 2010

Older versions of C#
The IntelliSense menu
The icon to the left of the word Console on the list above means that it is a Class. But press the Enter key on your keyboard. The word will be added to your code:
The C# keyword Console
Now type a full stop (period) immediately after the word Console. The IntelliSense menu appears again:
The Write Method
You can use the arrow keys on your keyboard to move up or down the list. But if you type Write and then the letter L of Line, IntelliSense will automatically move down and select it for you:
The WriteLine Method in C# .NET
Press the Enter key to add the word WriteLine to your code:
Your C# code
Now type a left round bracket. As soon as you type the round bracket, you'll see this:
The C# WriteLine options
WriteLine is another Method (A Method is just some code that does a particular job). But the yellow box is telling you that there are 19 different versions of this Method. You could click the small arrows to move up and down the list. Instead, type the following:
"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 text has been added to the C# code
Now type a right round bracket:
A round bracket has been added
Notice the red wiggly line at the end. This is the coding environment's way of telling you that you've missed something out.
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:
Note the semicolon at the end of the line
Note all the different colours. Visual C# colour-codes the different parts of your code. The reddish colour between double quotes means that you want text; the green colour means it's a Class; blue words are ones that C# reserves for itself.
(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:
The Output window in C# NET
The final line is this:
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
This time, you should see these two lines at the end of the report:
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:
Your C# Programme
And that's your programme! Have a look at the Solution Explorer on the right. Because the project has been built, you'll see two more files under Debug:
The Debug item in the Solution Explorer
However, in C# 2010 you'll see a Release folder. Expand this:
The build files in C# 2010
We now have a ConsoleApplication1.exe and ConsoleApplication1.pdb. The exe file is an executable programme, and it appears in the bin/debug folder. Switch back to Windows Explorer, if you still have it open. You'll see the exe file there. (In the Release folder in C# 2010, but in the Debug folder in earlier versions):
The files in the bin/debug folder
You could, if you wanted, create a desktop shortcut to this exe file. When you double click the desktop shortcut, the programme will run.

No comments:

Post a Comment