Share via


Introduction to projects and solutions

In this tutorial, you explore 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. You create a reference from one project to another in a solution. This tutorial also describes the properties of a project and some of the files that it can contain.

As an exercise to understand the concept of a project, you create a solution and project from scratch. Ordinarily, you'd use Visual Studio project templates to create new projects.

In this tutorial, you learn how to:

  • Add an item to the project
  • Add a second project
  • Add a project reference
  • Add test code
  • View Project properties

Prerequisites

Solutions and projects

In Visual Studio, a solution isn't an answer. A solution is simply a container that 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.

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.

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 often. When you create a new project, Visual Studio automatically creates a solution for the project unless a solution is already open.

Add a project

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

Add an item to the project

Add a code file to your empty project.

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

    The Add New Item dialog box opens. Select Show All Templates if dialog opens in compact view.

  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. 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.

Add a project reference

To use the new unit test project to test your method in the QuickDate project, you need to add a reference to QuickDate to the QuickTest project. Adding the reference creates a build dependency between the two projects. When you build the solution, QuickDate builds before QuickTest.

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

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

Tip

You can also open Test Explorer by choosing Test > 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.