Skip to content

Revised class library getting started topics for .NET Core 2.0 #2837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Learn how to call the members in a class library with Visual Studio
keywords: .NET Core, .NET Core class library, .NET Standard, .NET Standard class library distribution
author: BillWagner
ms.author: wiwang
ms.date: 04/17/2017
ms.date: 08/07/2017
ms.topic: article
ms.prod: .net
ms.technology: devlang-csharp
Expand All @@ -14,7 +14,7 @@ ms.assetid: d7b94076-1108-4174-94e7-a18f00072bb7

# Consuming a class library with .NET Core in Visual Studio 2017

Once you've followed the steps in [Building a C# class library with .NET Core in Visual Studio 2017](./library-with-visual-studio.md) and [Testing a class library with .NET Core in Visual Studio 2017](testing-library-with-visual-studio.md) to build and test your class library and you've built a Release version of the library, the next step is to make it available to callers. You can do this in two ways:
Once you've created a class library by following the steps in [Building a C# class library with .NET Core in Visual Studio 2017](./library-with-visual-studio.md) or [Building a Visual Basic class library with .NET Core in Visual Studio 2017](vb-library-with-visual-studio.md), tested it in [Testing a class library with .NET Core in Visual Studio 2017](testing-library-with-visual-studio.md), and built a Release version of the library, the next step is to make it available to callers. You can do this in two ways:

* If the library will be used by a single solution (for example, if it's a component in a single large application), you can include it as a project in your solution.

Expand All @@ -24,13 +24,14 @@ Once you've followed the steps in [Building a C# class library with .NET Core in

Just as you included unit tests in the same solution as your class library, you can include your application as part of that solution. For example, you can use your class library in a console application that prompts the user to enter a string and reports whether its first character is uppercase:

# [C#](#tab/csharp)
1. Open the `ClassLibraryProjects` solution you created in the [Building a C# Class Library with .NET Core in Visual Studio 2017](./library-with-visual-studio.md) topic. In **Solution Explorer**, right-click the **ClassLibraryProjects** solution and select **Add** > **New Project** from the context menu.

1. In the **Add New Project** dialog, select the **.NET Core** node followed by the **Console App (.NET Core)** project template. In the **Name** text box, type "ShowCase", and select the **OK** button.
1. In the **Add New Project** dialog, expand the **Visual C#** node and select the **.NET Core** node followed by the **Console App (.NET Core)** project template. In the **Name** text box, type "ShowCase", and select the **OK** button.

![Add New Project dialog](./media/consuming-library-with-visual-studio/addnewproject.png)

1. In **Solution Explorer**, right-click the **ShowCase** project and select **Set as StartUp Project** in the context menu.
1. In **Solution Explorer**, right-click the **ShowCase** project and select **Set as StartUp Project** in the context menu.

![ShowCase context menu](./media/consuming-library-with-visual-studio/setstartupproject.png)

Expand All @@ -44,7 +45,7 @@ Just as you included unit tests in the same solution as your class library, you

1. In the code window for the *Program.cs* file, replace all of the code with the following code:

[!CODE-csharp[UsingClassLib#1](../../../samples/snippets/csharp/getting_started/with_visual_studio_2017/showcase.cs#1)]
[!CODE-csharp[UsingClassLib#1](../../../samples/snippets/csharp/getting_started/with_visual_studio_2017/showcase.cs)]

The code uses the [Console.WindowHeight](xref:System.Console.WindowHeight) property to determine the number of rows in the console window. Whenever the [Console.CursorTop](xref:System.Console.CursorTop) property is greater than or equal to the number of rows in the console window, the code clears the console window and displays a message to the user.

Expand All @@ -53,8 +54,39 @@ Just as you included unit tests in the same solution as your class library, you
1. If necessary, change the toolbar to compile the **Debug** release of the `ShowCase` project. Compile and run the program by selecting the green arrow on the **ShowCase** button.

![Image](./media/consuming-library-with-visual-studio/toolbar.png)
# [Visual Basic](#tab/visual-basic)
1. Open the `ClassLibraryProjects` solution you created in the [Building a class Library with Visual Basic and .NET Core in Visual Studio 2017](vb-library-with-visual-studio.md) topic. In **Solution Explorer**, right-click the **ClassLibraryProjects** solution and select **Add** > **New Project** from the context menu.

You can debug and publish the application that uses this library by following the steps in [Debugging your C# Hello World application with Visual Studio 2017](debugging-with-visual-studio.md) and [Publishing your Hello World Application with Visual Studio 2017](publishing-with-visual-studio.md).
1. In the **Add New Project** dialog, expand the **Visual Basic** node and select the **.NET Core** node followed by the **Console App (.NET Core)** project template. In the **Name** text box, type "ShowCase", and select the **OK** button.

![Add New Project dialog](./media/consuming-library-with-visual-studio/vb-addnewproject.png)

1. In **Solution Explorer**, right-click the **ShowCase** project and select **Set as StartUp Project** in the context menu.

![ShowCase context menu](./media/consuming-library-with-visual-studio/setstartupproject.png)

1. Initially, your project doesn't have access to your class library. To allow it to call methods in your class library, you create a reference to the class library. In **Solution Explorer**, right-click the `ShowCase` project's **Dependencies** node and select **Add Reference**.

![ShowCase Dependencies context menu](./media/consuming-library-with-visual-studio/addreference.png)

1. In the **Reference Manager** dialog, select **StringLibrary**, your class library project, and select the **OK** button.

![Reference manager](./media/consuming-library-with-visual-studio/referencemanager.png)

1. In the code window for the *Program.vb* file, replace all of the code with the following code:

[!CODE-vb[UsingClassLib#1](../../../samples/snippets/core/tutorials/vb-library-with-visual-studio/showcase.vb)]

The code uses the [Console.WindowHeight](xref:System.Console.WindowHeight) property to determine the number of rows in the console window. Whenever the [Console.CursorTop](xref:System.Console.CursorTop) property is greater than or equal to the number of rows in the console window, the code clears the console window and displays a message to the user.

The program prompts the user to enter a string. It indicates whether the string starts with an uppercase character. If the user presses the Enter key without entering a string, the application terminates, and the console window closes.

1. If necessary, change the toolbar to compile the **Debug** release of the `ShowCase` project. Compile and run the program by selecting the green arrow on the **ShowCase** button.

![Image](./media/consuming-library-with-visual-studio/toolbar.png)
---

You can debug and publish the application that uses this library by following the steps in [Debugging your Hello World application with Visual Studio 2017](debugging-with-visual-studio.md) and [Publishing your Hello World Application with Visual Studio 2017](publishing-with-visual-studio.md).

## Distributing the library in a NuGet package

Expand Down
9 changes: 5 additions & 4 deletions docs/core/tutorials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ The following tutorials are available for learning about .NET Core.
- [Building a C# Hello World application](../../csharp/getting-started/with-visual-studio.md)
- [Debugging your C# Hello World application](../../csharp/getting-started/debugging-with-visual-studio.md)
- [Publishing your C# Hello World application](../../csharp/getting-started/publishing-with-visual-studio.md)
- [Building a C# class library](../../csharp/getting-started/library-with-visual-studio.md)
- [Testing a C# class library](../../csharp/getting-started/testing-library-with-visual-studio.md)
- [Consuming a C# class library with .NET Core](../../csharp/getting-started/consuming-library-with-visual-studio.md)
- [Building a C# class library](../../core/tutorials/library-with-visual-studio.md)
- [Building a class library with Visual Basic](../../core/tutorials/vb-library-with-visual-studio.md)
- [Testing a class library](../../core/tutorials/testing-library-with-visual-studio.md)
- [Consuming a class library](../../core/tutorials/consuming-library-with-visual-studio.md)
- [Building a complete C# .NET Core solution on Windows](using-on-windows-full-solution.md)
- [NoSQL tutorial: Build a DocumentDB C# console application on .NET Core](/azure/documentdb/documentdb-dotnetcore-get-started)

Expand All @@ -40,7 +41,7 @@ The following tutorials are available for learning about .NET Core.

- [Getting started with .NET Core on Windows/Linux/macOS using the .NET Core CLI tools](using-with-xplat-cli.md)
- [Organizing and testing projects with the .NET Core CLI tools](testing-with-cli.md)
- [Getting started with F#](../../fsharp/tutorials/getting-started/getting-started-command-line.md)
- [Get started with F#](../../fsharp/get-started/get-started-command-line.md)

## Other
- [Unit Testing in .NET Core using dotnet test](../testing/unit-testing-with-dotnet-test.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
---
title: Building a class library with C# and .NET Core in Visual Studio 2017
description: Learn how to build a class library written in C# using Visual Studio 2017
title: Building a .NET Standard class library with C# and .NET Core in Visual Studio 2017
description: Learn how to create a .NET Standard class library written in C# using Visual Studio 2017.
keywords: .NET Core, .NET Standard class library, Visual Studio 2017
author: BillWagner
ms.author: wiwagn
ms.date: 04/17/2017
ms.date: 08/07/2017
ms.topic: article
ms.prod: .net-core
ms.technology: devlang-csharp
ms.devlang: csharp
ms.assetid: c849ca26-6a25-4d35-9544-f343af88e0e7
---

# Building a class library with C# and .NET Core in Visual Studio 2017

A *class library* defines types and methods that are called by an application. A class library developed using .NET Core supports the .NET Standard, which allows your library to be called by any .NET implementation that supports that version of the .NET Standard. When you finish your class library, you can decide whether you want to distribute it as a third-party component or whether you want to include it as a bundled component with one or more applications.
A *class library* defines types and methods that are called by an application. A class library that targets the .NET Standard 2.0 allows your library to be called by any .NET implementation that supports that version of the .NET Standard. When you finish your class library, you can decide whether you want to distribute it as a third-party component or whether you want to include it as a bundled component with one or more applications.

> [!NOTE]
> For a list of the .NET Standard versions and the platforms they support, see [.NET Standard](../../standard/net-standard.md).

In this topic, you'll create a simple utility library that contains a single string-handling method. You'll implement it as an [extension method](../../csharp/programming-guide/classes-and-structs/extension-methods.md) so that you can call it as if it were a member of the @System.String class.
In this topic, you'll create a simple utility library that contains a single string-handling method. You'll implement it as an [extension method](../../csharp/programming-guide/classes-and-structs/extension-methods.md) so that you can call it as if it were a member of the <xref:System.String> class.

## Creating a class library solution

Expand All @@ -37,22 +36,28 @@ Create your class library project:

1. In **Solution Explorer**, right-click on the **ClassLibraryProjects** solution file and from the context menu, select **Add** > **New Project**.

1. In the **Add New Project** dialog, select the **.NET Core** node followed by the **Class Library (.NET Core)** project template. In the **Name** text box, enter "StringLibrary" as the name of the project. Select **OK** to create the class library project.
1. In the **Add New Project** dialog, expand the **Visual C#** node, then select the **.NET Standard** node followed by the **Class Library (.NET Standard)** project template. In the **Name** text box, enter "StringLibrary" as the name of the project. Select **OK** to create the class library project.

![Add New Project dialog](./media/library-with-visual-studio/libproject.png)

The code window then opens in the Visual Studio development environment.

![Visual Studio application window showing the default class library template code](./media/library-with-visual-studio/stringlibrary.png)

1. Check to make sure that our library targets the correct version of the .NET Standard. Right-click on the library project in the **Solution Explorer** windows, then select **Properties**. The **Target Framework** text box shows that we're targeting .NET Standard 2.0.

![Project properties for the class library](./media/library-with-visual-studio/properties.png)

1. Replace the code in the code window with the following code and save the file:

[!CODE-csharp[ClassLib#1](../../../samples/snippets/csharp/getting_started/with_visual_studio_2017/classlib.cs#1)]
[!CODE-csharp[ClassLib#1](../../../samples/snippets/csharp/getting_started/with_visual_studio_2017/classlib.cs)]

The class library, `UtilityLibraries.StringLibrary`, contains a method named `StartsWithUpper`, which returns a @System.Boolean value that indicates whether the current string instance begins with an uppercase character. The Unicode standard distinguishes uppercase characters from lowercase characters. In .NET Core, the [`Char.IsUpper`](xref:System.Char.IsUpper(System.Char)) method returns `true` if a character is uppercase.
The class library, `UtilityLibraries.StringLibrary`, contains a method named `StartsWithUpper`, which returns a <xref:System.Boolean> value that indicates whether the current string instance begins with an uppercase character. The Unicode standard distinguishes uppercase characters from lowercase characters. The <xref:System.Char.IsUpper(System.Char)?displayProperty=fullName> method returns `true` if a character is uppercase.

1. On the menu bar, select **Build** > **Build Solution**. The project should compile without error.

![Output pane showing that the build succeeded](./media/library-with-visual-studio/buildsucceeds.png)

## Next step

You've successfully built the library. Because you haven't called any of its methods, you don't know whether it works as expected. The next step in developing your library is to test it by using a [C# Unit Test Project](testing-library-with-visual-studio.md).
You've successfully built the library. Because you haven't called any of its methods, you don't know whether it works as expected. The next step in developing your library is to test it by using a [Unit Test Project](testing-library-with-visual-studio.md).
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading