Κοινή χρήση μέσω


Introduction to projects and solutions

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

This introductory article explores what it means to create a solution and a project in Visual Studio. A solution is a container to organize one or more related code projects, like a class library project and a corresponding test project.

As an educational exercise to understand the concept of a project, you'll construct a solution and project from scratch. Ordinarily, you'd use Visual Studio project templates to create new projects. You'll also look at the properties of a project and some of the files it can contain, and create a reference from one project to another.

Note

Developing apps in Visual Studio doesn't require solutions and projects. You can just open a folder that contains code and start coding, building, and debugging. For example, a cloned GitHub repo might not contain Visual Studio projects and solutions. For more information, see Develop code in Visual Studio without projects or solutions.

If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.

Solutions and projects

In Visual Studio, a solution isn't an "answer". A solution is simply a container Visual Studio uses to organize one or more related projects. When you open a solution, Visual Studio automatically loads all the projects that the solution contains.

Create a solution

Start your exploration by creating an empty solution. After you get to know Visual Studio, you probably won't create empty solutions very often. When you create a new project, Visual Studio automatically creates a solution for the project unless a solution is already open.

  1. Open Visual Studio.

  2. On the top menu bar, select File > New > Project.

    The New Project dialog box opens.

  3. In the left pane, expand Other Project Types, then select Visual Studio Solutions. In the center pane, select the Blank Solution template. Name your solution QuickSolution, then select the OK button.

    Screenshot that shows a Blank Solution template selected in Visual Studio 2017.

    The Start Page closes, and a solution appears in Solution Explorer on the right-hand side of the Visual Studio window. You'll probably use Solution Explorer often, to browse the contents of your projects.

Add a project

Now add your first project to the solution. Start with an empty project, and add the items you need.

  1. From the right-click or context menu of Solution 'QuickSolution' in Solution Explorer, select Add > New Project.

    The Add New Project dialog box opens.

  2. In the left pane, expand Visual C# and select Windows Desktop. Then, in the middle pane, select the Empty Project (.NET Framework) template. Name the project QuickDate, then select OK.

    A project named QuickDate appears beneath the solution in Solution Explorer. Currently it contains a single file called App.config.

    Note

    If you don't see Visual C# in the left pane of the dialog box, you must install the .NET desktop development Visual Studio workload. Visual Studio uses workload-based installation to install only the components you need for the type of development you do. An easy way to install a new workload is to select the Open Visual Studio Installer link in the bottom left corner of the Add New Project dialog box. After Visual Studio Installer launches, select the .NET desktop development workload and then the Modify button.

    Screenshot that shows the Open Visual Studio Installer link.

Add an item to the project

Add a code file to your empty project.

  1. From the right-click or context menu of the QuickDate project in Solution Explorer, select Add > New Item.

    The Add New Item dialog box opens.

  2. Expand Visual C# Items, and then select Code. In the middle pane, select the Class item template. Under Name, type Calendar, and then select Add.

    Visual Studio adds a file named Calendar.cs to the project. The .cs on the end is the file extension for C# code files. The Calendar.cs file appears in the Solution Explorer visual project hierarchy, and the file opens in the editor.

  3. Replace the contents of the Calendar.cs file with the following code:

    using System;
    
    namespace QuickDate
    {
        internal class Calendar
        {
            static void Main(string[] args)
            {
                DateTime now = GetCurrentDate();
                Console.WriteLine($"Today's date is {now}");
                Console.ReadLine();
            }
    
            internal static DateTime GetCurrentDate()
            {
                return DateTime.Now.Date;
            }
        }
    }
    

    You don't need to understand everything the code is doing yet. Run the app by pressing Ctrl+F5, and see that the app prints today's date to the console, or standard output, window. Then, close the console window.

Add a second project

Solutions commonly contain more than one project, and these projects often reference each other. Some projects in a solution might be class libraries, some might be executable applications, and some might be unit test projects or websites.

To add a unit test project to your solution, start from a project template so you don't have to add another code file to the project.

  1. From the right-click or context menu of Solution 'QuickSolution' in Solution Explorer, select Add > New Project.

  2. In the left pane, expand Visual C# and select the Test category. In the middle pane, select the MSTest Test Project (.NET Core) project template. Name the project QuickTest, and then select OK.

    A second project is added to Solution Explorer, and a file named UnitTest1.cs opens in the editor.

    Screenshot that shows Solution Explorer with two projects.

Add a project reference

You'll use the new unit test project to test your method in the QuickDate project, so you need to add a reference to QuickDate to the QuickTest project. Adding the reference creates a build dependency between the two projects, meaning that when you build the solution, QuickDate builds before QuickTest.

  1. Select the Dependencies node in the QuickTest project, and from the right-click or context menu, select Add Reference.

    The Reference Manager dialog box opens.

  2. In the left pane, expand Projects and select Solution. In the middle pane, select the checkbox next to QuickDate, and then select OK.

    A reference to the QuickDate project is added.

    A screenshot of Solution Explorer showing project reference in Visual Studio.

Add test code

  1. Now add test code to the C# test code file. Replace the contents of UnitTest1.cs with the following code:

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace QuickTest
    {
        [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestGetCurrentDate()
            {
                Assert.AreEqual(DateTime.Now.Date, QuickDate.Calendar.GetCurrentDate());
            }
        }
    }
    

    A red squiggle appears under some of the code. You can fix this error by making the test project a friend assembly to the QuickDate project.

  2. In the Calendar.cs file, add the following using statement and InternalsVisibleToAttribute attribute to the top of the file to resolve the error in the test project.

    using System.Runtime.CompilerServices;
    
    [assembly: InternalsVisibleTo("QuickTest")]
    

    The Calendar.cs code should look like this screenshot:

Run the unit test

If you want to check that your unit test is working, choose Test > Run > All Tests from the menu bar. A window called Test Explorer opens, and you should see that the TestGetCurrentDate test passes.

Tip

If Test Explorer doesn't open automatically, open it by choosing Test > Windows > Test Explorer from the menu bar.

Project properties

The line in the Calendar.cs file that contains the InternalsVisibleToAttribute attribute references the assembly name or file name of the QuickTest project. The assembly name might not always be the same as the project name. To find the assembly name of a project, use the project properties. The property pages contain various settings for the project.

  1. In Solution Explorer, right-click the QuickTest project and select Properties, or select the project and press Alt+Enter.

    The property pages for the project open to the Application tab. The Assembly name of the QuickTest project is indeed QuickTest.

    If you want, you can change the name here. When you build the test project, the name of the resulting binary file then changes from QuickTest.dll to <NewName>.dll.

  2. Explore some of the other tabs of the project's property pages, such as Build and Debug. These tabs are different for different types of projects.

See also