How to Begin C# Programming with Microsoft Visual Studio

parkmooseupvalleySoftware and s/w Development

Jul 5, 2012 (5 years and 4 months ago)

312 views

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004
How to Begin C# Programming with
Microsoft Visual Studio.NET

Jun Ni, Ph.D.M.E.
Department of Computer Science
The University of Iowa

(jun-ni@uiowa.edu)


Microsoft Visual Studio .NET (MVS.NET) is a convenient integrated development
environment (IDE),. It allows people to create, compile, run, and debug programs. It
supports different programming languages, including C#, C, C++, etc. In this quick guide,
we focus on how to use .NET to develop C# program.

1. Getting Started

After you open MVS.NET, it usually starts with a window similar to the one shown
following.



Figure 1. MVS.NET Start Window

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004
The appearance of the Start Page or Window may be different, but very similar,
depending upon the initial or customer system setting.


Each job of development should be performed within a project. You can either open an
existing project or create a new project.

1.1. Create New Project
If you are working on an existing project which already allocated on your computer, you
can skip this step. If you want to create a new project, you can



Figure 2. Menu to create new project

Or you can use Ctrl-Shift+N or click New Project Icon, shown in Figure 3. You can
also click New Project Button in Projects Tab to create new project, shown in Figure 4.
How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004


Figure 3. Icon in toolbar to create new project.



Figure 4. New Project Button in Projects Tab to create new project.

After you select one of these options, a New Project Window prompts out, which is
shown in Figure 5.

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004


Figure 5. New Project Window which allows user to select project type.

From the window, you can see there are many options, that allow users to create different
types of project, such as Visual Basic, Visual C#, Visual C/C++, and others.

At this moment, we will select Console Application Template. The Console
Application Template enhanced by Console Application Icon can be found by sliding
down the Sliding Bar of Templates, as shown in Figure 6.


How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004

.

Figure 6. Select Console Application Template to define the type of new project for
console application.

Now you can name your project. You can also save your project file into your preferred
directory by click Browse… button, shown in Figure 7.



Figure 7. Project Location Window to specify the directory in which the project file is
saved.
How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004
Now MVS.NET gives you a new window with the project name you specified, shown in
Figure 8.



Figure 8. Programming File Edit Window which allows users to edit or develop code.

Now you can edit your program. First, let's delete " namespace FirstProject" (as
default, it is given as the project name) and its scope curly brackets {}. You can leave it
alone. I will explain it late.

Let's modify the default one as a simple C# code. Type the code listed on Page 4 of the
text book.

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004
// Hello world in C#
// by Jun Ni

using System;
class Hello
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}

Also delete "[STATthread]", which is indented to indicate when the execution starts.
You can also delete "string[] args" within the parenthesis within the Main(), if one
does't want to pass arguments to the code (as we will explain late), as well as other
unnecessary comments. Now the code looks like



Figure 9. Simplest "hello" code defines a "FirstHello" class.

1.2 Compile and Load Functions

If there are no syntax errors, you can compile and load functions on the edited program.
There are two major options which you can select. They are Build Project and Build
Solution options. In the current study, it doesn't matter. Basically Build Project performs
the compilation and loading over all the files associated with the current project, while
How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004
Build Solution performs the compilation and loading on not only the files associated
with the current project, but also other projects even with the file using. The Build menu
gives

Figure 10. Build menu.

Either Build Solution or Build FirstProject submenus (the name of current project)
gives the following successful result, shown in Figure 11.



Figure 11. Success of building current project.
How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004

1.3 Run or Execute Program

Now you can run (execute) your code using Debug menu. There are four ways to run
your code. The basic way is to run your code without debugging.

1.3.1 Go to Debug Menu and select Start Without Debugging (Ctrl+F5) (shown in
Figure 1.2) to enable an execution window (Figure 13).



Figure 12. Debug Menu




Figure 13. Console application result window. Enter any key to close the result window.

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004
If user selects Start submenu (F5), it puts up the Execution Window only while the
program is actually running. It will disappear once the program completes, so user can
not check the results statically displayed on the window. In order to check the execute
status, you need to set a breakpoint.

Now we can close the project by clicking the Close Solution submenu in File Menu
shown in Figure 14.



Figure 14. Close solution (project).

1.4 Open Existing Project

If you have existing project which already developed and your would like to load to the
current MVS.NET, you can select Project… Menu Item in Open submenu of File Menu.

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004


Figure 1.5 Open existing project.

It prompts Open Project Window which allows users to find the existing project file,
named as Project Name with extension .csproj. For example, we can search the location
of our FirstProject saved and find the project file called FirstProject.csproj, shown in
Figure 16.



Figure 16. Open the existing project file.
How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004
Now you have the project with a C# file called Class1.cs
associated with the project. It
should be noted that the file name of the C# may be different from the class defined in the
file. In this case, we define a class called FirstHello. This is different from Java, in which
the file name has to be identical to the name of class.

1.5 View Opened Project (solution) using Solution Explorer

After you open an existing project (solution) you can view the contents of the project by
click View Menu and Solution Explorer (Ctrl_Alt-L), shown in Figure 17. The Solution
Explorer gives display the contents of the current project (solution) opened, shown in
Figure 18. It allows user to delete loaded C# file within the project and display the
updated new C# file.



Figure 17. Access the Solution Explorer.


How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004



Figure 18. Solution Explorer.

For example, you can add new C# code by selecting Add New Item…
(Ctrl+Shift+A)(shown in Figure 19), which gives a new window (Figure 20).



Figure 19. Add New Item Submenu.

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004


Figure 20. Add New Item Window which has many options of item types.

Let's select TextFile and save the file name as FirstHello.cs, which will be saved and
added into the current project, shown in Solution Explorer Window.

Alternatively, you can use File Menu and New to create a new C# file, shown in Figure
21. It gives a new window.



Figure 21. Create a new C# file under File Menu and then New Submenu.

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004


Figure 22. New File window.

Click Open Button, it give you a subwindow within the MVS.NET, shown in Figure 22.
This allows user to type and edit a new C# file and then save it with extension name .cs,
shown in Figure 23 and Figure 24. It should be noted that the file is not necessarily saved
into the project folder. It can be saved into any directory and late add to the project as we
will discuss.

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004


Figure 23. new edit window allows user to type C# code and save it as a new C# file.



Figure 24. Save new C# file using Save As.. Submenu.

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004


Figure 24. Locate the directory and save the new file as C# file.

1.6 Update C# File Within the Current Opened Project.

Once you have existing C# file, you can update or replace the current one within the
project. First, you need to delete one you want to be replaced. Say, useHello.cs. You can
highlight it and press Delete key or use menu function to delete the file as shown in
Figure 25.



How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004
Figure 25, Delete file in the project.

It gives you a chance to confirm,, shown in Figure 26.



Figure 26. Confirmation of Delete C# file from the opend project.

Now you can add the existing C# file to the currently opened project by monitoring
through the Solution Explorer. You can click Project Menu and then Add Existing
Item.. Submenu, shown in Figure 27. The option gives a new window to find the existing
C# file, shown in Figure 28.



Figure 27. Add Existing Item… Submenu.

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004


Figure 28. Load existing C# file into the opened project.

Now the Solution Explorer contains the newly added C3 file called UseHello2.cs, shown
in Figure 29.




Figure 29. Solution Explorer displays the newly added C# file.

It is time to compile and run the project.

How to Develop Console Application using C# in Microsoft Visual Studio .NET Jun Ni
Jan. 20, 2004
1.7 Close Project

After you accomplish your task, you need to save all and close the solution or project.




Figure 30. Close Solution or just Close.