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, such as the source code files, 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 by using the files in your 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, the Visual Web Developer integrated 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 see Editing Code (Visual C#) for information about the C# editor.
The following illustration shows the New Project dialog box. By default, Visual C# is selected in the window on the left, and on the right, there are project templates to select from. If you expand and click a node on the left, you can see different project types appear on the right side. The list of project templates depends in part on the version of Visual Studio that you have.
You can select the version of the .NET Framework for your project in the New Project dialog box. The version of the .NET Framework that you select in the New Project dialog box is the version that is required on a computer to enable it to run the application. For more information, see Visual Studio Multi-Targeting Overview.
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 Solution Explorer to the right.
The Properties node represents configuration settings that apply to your whole project and are stored in the .csproj file in your solution folder. These settings include compilation options, security settings, deployment settings, and much more. You modify your project by 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 Modifying Project Properties (Visual C#).
In the context of a project, a reference 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 another assembly, 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. By default, all C# projects 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 by 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 instead of 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 as follows: strings, images, icons, audio, and files. You add, remove, or edit resources by using the Resource Designer, which can be accessed on the Resources tab in the Project Designer.
When you create a Windows Forms project, Visual C# adds one form to the project 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 command, 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 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 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.