|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Creating a Project (Visual C#)
When you are ready to begin coding, the first step is to set up a project. The project contains all the raw materials for your application, including not only the source code files, but also resource files such as icons, references to external files that your program depends on, and configuration data such as compiler settings. When you build a project, Visual C# invokes the C# compiler and other internal tools to create an executable assembly using the files in your project.
Creating a New Project
You create a new project by clicking the File menu, pointing to New, and then clicking Project.
If you select Web Site instead of Project, theintegrated development environment (IDE) opens. This is a separate and distinct environment within Visual Studio for creating ASP.NET Web applications. The Visual Web Developer IDE does use the Visual C# code editor for editing code-behind files in C#. If you are creating Web applications, you should use the Visual Web Developer documentation primarily, but refer to for information about the C# editor.
The following illustration shows the New Project dialog box. You can see that Visual C# is selected by default in the window on the left, and on the right, you have a choice of six or more project templates to choose from. If you expand the Smart Device or Other Project Types node on the left, you can see different project types appear on the right side.
Starter Kits are another type of project template. If you install a starter kit, you will see it listed in the New Project Dialog Box. For more information, see.
After you select a project template and click OK, Visual Studio creates the project and you are ready to begin coding. The project files, references, settings, and resources are visible in the Solution Explorer window to the right.
What's in Your Project?
The Properties node represents configuration settings that apply to your entire project and are stored in the .csproj file in your solution folder. These settings include compilation options, security and deployment settings and much more. You make modifications to your project using the Project Designer, which is a set of Property Pages that you access by right-clicking Properties, and selecting Open. For more information, see.
In the context of a project, a reference simply identifies a binary file that your application requires in order to run. Typically, a reference identifies a DLL file such as one of the .NET Framework class library files. It can also reference a .NET assembly (called a shim) that enables your application to call methods on a COM object or native Win32 DLL. If your program creates an instance of a class that is defined in some other assembly, then you must add a reference to that file in your project before you compile your project. To add a reference, click Add Reference on the Project menu. All C# projects by default include a reference to mscorlib.dll, which contains the core .NET Framework classes. You can add references to additional .NET Framework DLLs and other files by clicking the Project menu, and selecting Add Reference.
Do not confuse the concept of a project reference with the concept of reference types in C# or other programming languages. The former refers to a file and its expected location on disk. The latter refers to C# types, which are declared using the class keyword.
A resource is data that is included with your application but can be stored in such a way that it can be modified independently from the other source code. For example, you can store all your strings as resources rather than hard-coding them into the source code. You can then translate the strings into different languages at some later date, and add them to the application folder that you ship to customers without having to recompile your assembly. The five types of resources defined by Visual C# are: strings, images, icons, audio, and files. You add, remove, or edit resources using the Resource Designer, which is accessed on the Resources tab in the Project Designer.
When you create a Windows Forms project, Visual C# adds one form to the project by default and calls it Form1. The two files that represent the form are called Form1.cs and Form1.designer.cs. You write your code in Form1.cs; the designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by dragging and dropping controls from the Toolbox.
You can add a new form by clicking the Project menu item, and selecting Add Windows Form. Each form has two files associated with it. Form1.cs, or whatever you might name it, contains the source code that you write to configure the form and its controls, such as list boxes and text boxes, and responds to events such as button clicks and keystrokes. In simple Windows Forms projects, you do most or all of your coding in this file.
The Designer.cs file contains the source code that the Forms Designer writes when you drag controls onto the form, set properties in the Properties window, and so on. Typically, you should not edit this file manually at all.
Obviously, if you create a console application project, it will not contain source code files for Windows Forms.
Other Source Code Files
A project may include any number of additional .cs files that may or may not be associated with a particular Windows Form. In the previous illustration of Solution Explorer, program.cs contains the entry point for the application. A single .cs file can contain any number of class and struct definitions. You can add new or existing files or classes to your project by clicking Add New Item or Add Existing Item on the Project menu.