다음을 통해 공유


자습서: Visual Studio Code를 사용하여 .NET 클래스 라이브러리 만들기

이 자습서에서는 단일 문자열 처리 메서드를 포함하는 간단한 유틸리티 라이브러리를 만듭니다.

클래스 라이브러리는 애플리케이션에서 호출되는 형식 및 메서드를 정의합니다. 라이브러리가 .NET Standard 2.0을 대상으로 하는 경우 .NET Standard 2.0을 지원하는 .NET 구현(.NET Framework 포함)에서 호출될 수 있습니다. 라이브러리가 .NET 8을 대상으로 하는 경우 .NET 8을 대상으로 하는 모든 애플리케이션에서 호출될 수 있습니다. 이 자습서에서는 .NET 8을 대상으로 지정하는 방법을 보여 줍니다.

클래스 라이브러리를 만들 때 타사 구성 요소 또는 하나 이상의 애플리케이션이 포함된 번들 구성 요소로 배포할 수 있습니다.

사전 요구 사항

솔루션 만들기

먼저 클래스 라이브러리 프로젝트를 배치할 빈 솔루션을 만듭니다. 솔루션은 하나 이상의 프로젝트에 대한 컨테이너로 작동합니다. 동일한 솔루션에 관련 프로젝트를 추가합니다.

  1. Visual Studio Code를 시작합니다.

  2. 주 메뉴에서 파일>폴더 열기 (macOS에서는 열기... )를 선택합니다.

  3. 폴더 열기 대화 상자에서 ClassLibraryProjects 폴더를 만들고 폴더 선택(macOS에서는 열기)을 클릭합니다.

  4. 주 메뉴에서 보기>터미널을 선택하여 Visual Studio Code에서 터미널을 엽니다.

    ClassLibraryProjects 폴더에서 명령 프롬프트와 함께 터미널이 열립니다.

  5. 터미널에서 다음 명령을 입력합니다.

    dotnet new sln
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    The template "Solution File" was created successfully.
    

클래스 라이브러리 프로젝트 만들기

“StringLibrary”라는 새로운 .NET 클래스 라이브러리 프로젝트를 솔루션에 추가합니다.

  1. 터미널에서 다음 명령을 실행하여 라이브러리 프로젝트를 만듭니다.

    dotnet new classlib -o StringLibrary
    

    -o 또는 --output 명령은 생성된 출력을 저장할 위치를 지정합니다.

    터미널 출력은 다음 예제처럼 표시됩니다.

    The template "Class library" was created successfully.
    Processing post-creation actions...
    Running 'dotnet restore' on StringLibrary\StringLibrary.csproj...
      Determining projects to restore...
      Restored C:\Projects\ClassLibraryProjects\StringLibrary\StringLibrary.csproj (in 328 ms).
    Restore succeeded.
    
  2. 다음 명령을 실행하여 라이브러리 프로젝트를 솔루션에 추가합니다.

    dotnet sln add StringLibrary/StringLibrary.csproj
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Project `StringLibrary\StringLibrary.csproj` added to the solution.
    
  3. 라이브러리가 .NET 8을 대상으로 하는지 확인합니다. Explorer에서 StringLibrary/StringLibrary.csproj를 엽니다.

    TargetFramework 요소는 프로젝트가 .NET 8.0을 대상으로 함을 보여 줍니다.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
      </PropertyGroup>
    
    </Project>
    
  4. Class1.cs를 열고 코드를 다음 코드로 바꿉니다.

    namespace UtilityLibraries;
    
    public static class StringLibrary
    {
        public static bool StartsWithUpper(this string? str)
        {
            if (string.IsNullOrWhiteSpace(str))
                return false;
    
            char ch = str[0];
            return char.IsUpper(ch);
        }
    }
    

    클래스 라이브러리 UtilityLibraries.StringLibrary에는 StartsWithUpper라는 메서드가 포함되어 있습니다. 이 메서드는 현재 문자열 인스턴스가 대문자로 시작하는지 여부를 나타내는 Boolean 값을 반환합니다. 유니코드 표준은 대문자와 소문자를 구분합니다. Char.IsUpper(Char) 메서드는 문자가 대문자인 경우 true를 반환합니다.

    StartsWithUpperString 클래스의 멤버인 것처럼 호출할 수 있도록 확장 메서드로 구현됩니다.

  5. 파일을 저장합니다.

  6. 다음 명령을 실행하여 솔루션을 빌드하고 프로젝트가 오류 없이 컴파일되는지 확인합니다.

    dotnet build
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Microsoft (R) Build Engine version 17.8.0+b89cb5fde for .NET
    Copyright (C) Microsoft Corporation. All rights reserved.
      Determining projects to restore...
      All projects are up-to-date for restore.
      StringLibrary -> C:\Projects\ClassLibraryProjects\StringLibrary\bin\Debug\net8.0\StringLibrary.dll
    Build succeeded.
        0 Warning(s)
        0 Error(s)
    Time Elapsed 00:00:02.78
    

솔루션에 콘솔 앱 추가

클래스 라이브러리를 사용하는 콘솔 애플리케이션을 추가합니다. 이 앱은 사용자에게 문자열을 입력하라는 메시지를 표시하고 문자열이 대문자로 시작하는지 여부를 보고합니다.

  1. 터미널에서 다음 명령을 실행하여 콘솔 앱 프로젝트를 만듭니다.

    dotnet new console -o ShowCase
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    The template "Console Application" was created successfully.
    Processing post-creation actions...
    Running 'dotnet restore' on ShowCase\ShowCase.csproj...
      Determining projects to restore...
      Restored C:\Projects\ClassLibraryProjects\ShowCase\ShowCase.csproj (in 210 ms).
    Restore succeeded.
    
  2. 다음 명령을 실행하여 솔루션에 콘솔 앱 프로젝트를 추가합니다.

    dotnet sln add ShowCase/ShowCase.csproj
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Project `ShowCase\ShowCase.csproj` added to the solution.
    
  3. ShowCase/Program.cs를 열고 모든 코드를 다음 코드로 바꿉니다.

    using UtilityLibraries;
    
    class Program
    {
        static void Main(string[] args)
        {
            int row = 0;
    
            do
            {
                if (row == 0 || row >= 25)
                    ResetConsole();
    
                string? input = Console.ReadLine();
                if (string.IsNullOrEmpty(input)) break;
                Console.WriteLine($"Input: {input}");
                Console.WriteLine("Begins with uppercase? " +
                     $"{(input.StartsWithUpper() ? "Yes" : "No")}");
                Console.WriteLine();
                row += 4;
            } while (true);
            return;
    
            // Declare a ResetConsole local method
            void ResetConsole()
            {
                if (row > 0)
                {
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
                Console.Clear();
                Console.WriteLine($"{Environment.NewLine}Press <Enter> only to exit; otherwise, enter a string and press <Enter>:{Environment.NewLine}");
                row = 3;
            }
        }
    }
    

    코드는 row 변수를 사용하여 콘솔 창에 기록된 데이터 행 수를 유지합니다. 25보다 크거나 같으면 코드는 콘솔 창을 지우고 사용자에게 메시지를 표시합니다.

    프로그램에서 문자열을 입력하라는 메시지를 사용자에게 표시합니다. 문자열이 대문자로 시작하는지 여부를 나타냅니다. 사용자가 문자열을 입력하지 않고 Enter 키를 누르면 애플리케이션이 종료되고 콘솔 창이 닫힙니다.

  4. 변경 내용을 저장합니다.

프로젝트 참조 추가

처음에는 새 콘솔 앱 프로젝트가 클래스 라이브러리에 액세스할 수 없습니다. 클래스 라이브러리의 메서드를 호출할 수 있도록 허용하려면 클래스 라이브러리 프로젝트에 대한 프로젝트 참조를 만듭니다.

  1. 다음 명령을 실행합니다.

    dotnet add ShowCase/ShowCase.csproj reference StringLibrary/StringLibrary.csproj
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Reference `..\StringLibrary\StringLibrary.csproj` added to the project.
    

앱 실행

  1. 터미널에서 다음 명령을 실행합니다.

    dotnet run --project ShowCase/ShowCase.csproj
    
  2. 문자열을 입력하고 Enter 키를 눌러 프로그램을 사용해 본 다음 Enter 키를 눌러 끝냅니다.

    터미널 출력은 다음 예제처럼 표시됩니다.

    Press <Enter> only to exit; otherwise, enter a string and press <Enter>:
    
    A string that starts with an uppercase letter
    Input: A string that starts with an uppercase letter
    Begins with uppercase? : Yes
    
    a string that starts with a lowercase letter
    Input: a string that starts with a lowercase letter
    Begins with uppercase? : No
    

추가 자료

다음 단계

이 자습서에서는 솔루션을 만들고, 라이브러리 프로젝트를 추가하고, 라이브러리를 사용하는 콘솔 앱 프로젝트를 추가했습니다. 다음 자습서에서는 솔루션에 단위 테스트 프로젝트를 추가합니다.

이 자습서에서는 단일 문자열 처리 메서드를 포함하는 간단한 유틸리티 라이브러리를 만듭니다.

클래스 라이브러리는 애플리케이션에서 호출되는 형식 및 메서드를 정의합니다. 라이브러리가 .NET Standard 2.0을 대상으로 하는 경우 .NET Standard 2.0을 지원하는 .NET 구현(.NET Framework 포함)에서 호출될 수 있습니다. 라이브러리가 .NET 7을 대상으로 하는 경우 .NET 7을 대상으로 하는 모든 애플리케이션에서 라이브러리를 호출할 수 있습니다. 이 자습서에서는 .NET 7을 대상으로 지정하는 방법을 보여줍니다.

클래스 라이브러리를 만들 때 타사 구성 요소 또는 하나 이상의 애플리케이션이 포함된 번들 구성 요소로 배포할 수 있습니다.

사전 요구 사항

솔루션 만들기

먼저 클래스 라이브러리 프로젝트를 배치할 빈 솔루션을 만듭니다. 솔루션은 하나 이상의 프로젝트에 대한 컨테이너로 작동합니다. 동일한 솔루션에 관련 프로젝트를 추가합니다.

  1. Visual Studio Code를 시작합니다.

  2. 주 메뉴에서 파일>폴더 열기 (macOS에서는 열기... )를 선택합니다.

  3. 폴더 열기 대화 상자에서 ClassLibraryProjects 폴더를 만들고 폴더 선택(macOS에서는 열기)을 클릭합니다.

  4. 주 메뉴에서 보기>터미널을 선택하여 Visual Studio Code에서 터미널을 엽니다.

    ClassLibraryProjects 폴더에서 명령 프롬프트와 함께 터미널이 열립니다.

  5. 터미널에서 다음 명령을 입력합니다.

    dotnet new sln
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    The template "Solution File" was created successfully.
    

클래스 라이브러리 프로젝트 만들기

“StringLibrary”라는 새로운 .NET 클래스 라이브러리 프로젝트를 솔루션에 추가합니다.

  1. 터미널에서 다음 명령을 실행하여 라이브러리 프로젝트를 만듭니다.

    dotnet new classlib -o StringLibrary
    

    -o 또는 --output 명령은 생성된 출력을 저장할 위치를 지정합니다.

    터미널 출력은 다음 예제처럼 표시됩니다.

    The template "Class library" was created successfully.
    Processing post-creation actions...
    Running 'dotnet restore' on StringLibrary\StringLibrary.csproj...
      Determining projects to restore...
      Restored C:\Projects\ClassLibraryProjects\StringLibrary\StringLibrary.csproj (in 328 ms).
    Restore succeeded.
    
  2. 다음 명령을 실행하여 라이브러리 프로젝트를 솔루션에 추가합니다.

    dotnet sln add StringLibrary/StringLibrary.csproj
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Project `StringLibrary\StringLibrary.csproj` added to the solution.
    
  3. 라이브러리가 .NET 7을 대상으로 하는지 확인합니다. Explorer에서 StringLibrary/StringLibrary.csproj를 엽니다.

    TargetFramework 요소는 프로젝트가 .NET 7.0을 대상으로 함을 보여줍니다.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
      </PropertyGroup>
    
    </Project>
    
  4. Class1.cs를 열고 코드를 다음 코드로 바꿉니다.

    namespace UtilityLibraries;
    
    public static class StringLibrary
    {
        public static bool StartsWithUpper(this string? str)
        {
            if (string.IsNullOrWhiteSpace(str))
                return false;
    
            char ch = str[0];
            return char.IsUpper(ch);
        }
    }
    

    클래스 라이브러리 UtilityLibraries.StringLibrary에는 StartsWithUpper라는 메서드가 포함되어 있습니다. 이 메서드는 현재 문자열 인스턴스가 대문자로 시작하는지 여부를 나타내는 Boolean 값을 반환합니다. 유니코드 표준은 대문자와 소문자를 구분합니다. Char.IsUpper(Char) 메서드는 문자가 대문자인 경우 true를 반환합니다.

    StartsWithUpperString 클래스의 멤버인 것처럼 호출할 수 있도록 확장 메서드로 구현됩니다.

  5. 파일을 저장합니다.

  6. 다음 명령을 실행하여 솔루션을 빌드하고 프로젝트가 오류 없이 컴파일되는지 확인합니다.

    dotnet build
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Microsoft (R) Build Engine version 16.7.4+b89cb5fde for .NET
    Copyright (C) Microsoft Corporation. All rights reserved.
      Determining projects to restore...
      All projects are up-to-date for restore.
      StringLibrary -> C:\Projects\ClassLibraryProjects\StringLibrary\bin\Debug\net7.0\StringLibrary.dll
    Build succeeded.
        0 Warning(s)
        0 Error(s)
    Time Elapsed 00:00:02.78
    

솔루션에 콘솔 앱 추가

클래스 라이브러리를 사용하는 콘솔 애플리케이션을 추가합니다. 이 앱은 사용자에게 문자열을 입력하라는 메시지를 표시하고 문자열이 대문자로 시작하는지 여부를 보고합니다.

  1. 터미널에서 다음 명령을 실행하여 콘솔 앱 프로젝트를 만듭니다.

    dotnet new console -o ShowCase
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    The template "Console Application" was created successfully.
    Processing post-creation actions...
    Running 'dotnet restore' on ShowCase\ShowCase.csproj...
      Determining projects to restore...
      Restored C:\Projects\ClassLibraryProjects\ShowCase\ShowCase.csproj (in 210 ms).
    Restore succeeded.
    
  2. 다음 명령을 실행하여 솔루션에 콘솔 앱 프로젝트를 추가합니다.

    dotnet sln add ShowCase/ShowCase.csproj
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Project `ShowCase\ShowCase.csproj` added to the solution.
    
  3. ShowCase/Program.cs를 열고 모든 코드를 다음 코드로 바꿉니다.

    using UtilityLibraries;
    
    class Program
    {
        static void Main(string[] args)
        {
            int row = 0;
    
            do
            {
                if (row == 0 || row >= 25)
                    ResetConsole();
    
                string? input = Console.ReadLine();
                if (string.IsNullOrEmpty(input)) break;
                Console.WriteLine($"Input: {input}");
                Console.WriteLine("Begins with uppercase? " +
                     $"{(input.StartsWithUpper() ? "Yes" : "No")}");
                Console.WriteLine();
                row += 4;
            } while (true);
            return;
    
            // Declare a ResetConsole local method
            void ResetConsole()
            {
                if (row > 0)
                {
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
                Console.Clear();
                Console.WriteLine($"{Environment.NewLine}Press <Enter> only to exit; otherwise, enter a string and press <Enter>:{Environment.NewLine}");
                row = 3;
            }
        }
    }
    

    코드는 row 변수를 사용하여 콘솔 창에 기록된 데이터 행 수를 유지합니다. 25보다 크거나 같으면 코드는 콘솔 창을 지우고 사용자에게 메시지를 표시합니다.

    프로그램에서 문자열을 입력하라는 메시지를 사용자에게 표시합니다. 문자열이 대문자로 시작하는지 여부를 나타냅니다. 사용자가 문자열을 입력하지 않고 Enter 키를 누르면 애플리케이션이 종료되고 콘솔 창이 닫힙니다.

  4. 변경 내용을 저장합니다.

프로젝트 참조 추가

처음에는 새 콘솔 앱 프로젝트가 클래스 라이브러리에 액세스할 수 없습니다. 클래스 라이브러리의 메서드를 호출할 수 있도록 허용하려면 클래스 라이브러리 프로젝트에 대한 프로젝트 참조를 만듭니다.

  1. 다음 명령을 실행합니다.

    dotnet add ShowCase/ShowCase.csproj reference StringLibrary/StringLibrary.csproj
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Reference `..\StringLibrary\StringLibrary.csproj` added to the project.
    

앱 실행

  1. 터미널에서 다음 명령을 실행합니다.

    dotnet run --project ShowCase/ShowCase.csproj
    
  2. 문자열을 입력하고 Enter 키를 눌러 프로그램을 사용해 본 다음 Enter 키를 눌러 끝냅니다.

    터미널 출력은 다음 예제처럼 표시됩니다.

    Press <Enter> only to exit; otherwise, enter a string and press <Enter>:
    
    A string that starts with an uppercase letter
    Input: A string that starts with an uppercase letter
    Begins with uppercase? : Yes
    
    a string that starts with a lowercase letter
    Input: a string that starts with a lowercase letter
    Begins with uppercase? : No
    

추가 자료

다음 단계

이 자습서에서는 솔루션을 만들고, 라이브러리 프로젝트를 추가하고, 라이브러리를 사용하는 콘솔 앱 프로젝트를 추가했습니다. 다음 자습서에서는 솔루션에 단위 테스트 프로젝트를 추가합니다.

이 자습서에서는 단일 문자열 처리 메서드를 포함하는 간단한 유틸리티 라이브러리를 만듭니다.

클래스 라이브러리는 애플리케이션에서 호출되는 형식 및 메서드를 정의합니다. 라이브러리가 .NET Standard 2.0을 대상으로 하는 경우 .NET Standard 2.0을 지원하는 .NET 구현(.NET Framework 포함)에서 호출될 수 있습니다. 라이브러리가 .NET 6을 대상으로 하는 경우 .NET 6을 대상으로 하는 모든 애플리케이션에서 호출될 수 있습니다. 이 자습서에서는 .NET 6을 대상으로 지정하는 방법을 보여 줍니다.

클래스 라이브러리를 만들 때 타사 구성 요소 또는 하나 이상의 애플리케이션이 포함된 번들 구성 요소로 배포할 수 있습니다.

사전 요구 사항

솔루션 만들기

먼저 클래스 라이브러리 프로젝트를 배치할 빈 솔루션을 만듭니다. 솔루션은 하나 이상의 프로젝트에 대한 컨테이너로 작동합니다. 동일한 솔루션에 관련 프로젝트를 추가합니다.

  1. Visual Studio Code를 시작합니다.

  2. 주 메뉴에서 파일>폴더 열기 (macOS에서는 열기... )를 선택합니다.

  3. 폴더 열기 대화 상자에서 ClassLibraryProjects 폴더를 만들고 폴더 선택(macOS에서는 열기)을 클릭합니다.

  4. 주 메뉴에서 보기>터미널을 선택하여 Visual Studio Code에서 터미널을 엽니다.

    ClassLibraryProjects 폴더에서 명령 프롬프트와 함께 터미널이 열립니다.

  5. 터미널에서 다음 명령을 입력합니다.

    dotnet new sln
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    The template "Solution File" was created successfully.
    

클래스 라이브러리 프로젝트 만들기

“StringLibrary”라는 새로운 .NET 클래스 라이브러리 프로젝트를 솔루션에 추가합니다.

  1. 터미널에서 다음 명령을 실행하여 라이브러리 프로젝트를 만듭니다.

    dotnet new classlib -f net6.0 -o StringLibrary
    

    -f 또는 --framework 명령은 기본 대상 프레임워크를 net6.0 버전으로 변경합니다.

    -o 또는 --output 명령은 생성된 출력을 저장할 위치를 지정합니다.

    터미널 출력은 다음 예제처럼 표시됩니다.

    The template "Class library" was created successfully.
    Processing post-creation actions...
    Running 'dotnet restore' on StringLibrary\StringLibrary.csproj...
      Determining projects to restore...
      Restored C:\Projects\ClassLibraryProjects\StringLibrary\StringLibrary.csproj (in 328 ms).
    Restore succeeded.
    
  2. 다음 명령을 실행하여 라이브러리 프로젝트를 솔루션에 추가합니다.

    dotnet sln add StringLibrary/StringLibrary.csproj
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Project `StringLibrary\StringLibrary.csproj` added to the solution.
    
  3. 라이브러리가 .NET 6를 대상으로 하는지 확인합니다. Explorer에서 StringLibrary/StringLibrary.csproj를 엽니다.

    TargetFramework 요소는 프로젝트가 .NET 6.0을 대상으로 함을 보여줍니다.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
      </PropertyGroup>
    
    </Project>
    
  4. Class1.cs를 열고 코드를 다음 코드로 바꿉니다.

    namespace UtilityLibraries;
    
    public static class StringLibrary
    {
        public static bool StartsWithUpper(this string? str)
        {
            if (string.IsNullOrWhiteSpace(str))
                return false;
    
            char ch = str[0];
            return char.IsUpper(ch);
        }
    }
    

    클래스 라이브러리 UtilityLibraries.StringLibrary에는 StartsWithUpper라는 메서드가 포함되어 있습니다. 이 메서드는 현재 문자열 인스턴스가 대문자로 시작하는지 여부를 나타내는 Boolean 값을 반환합니다. 유니코드 표준은 대문자와 소문자를 구분합니다. Char.IsUpper(Char) 메서드는 문자가 대문자인 경우 true를 반환합니다.

    StartsWithUpperString 클래스의 멤버인 것처럼 호출할 수 있도록 확장 메서드로 구현됩니다.

  5. 파일을 저장합니다.

  6. 다음 명령을 실행하여 솔루션을 빌드하고 프로젝트가 오류 없이 컴파일되는지 확인합니다.

    dotnet build
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET
    Copyright (C) Microsoft Corporation. All rights reserved.
      Determining projects to restore...
      All projects are up-to-date for restore.
      StringLibrary -> C:\Projects\ClassLibraryProjects\StringLibrary\bin\Debug\net6.0\StringLibrary.dll
    Build succeeded.
        0 Warning(s)
        0 Error(s)
    Time Elapsed 00:00:02.78
    

솔루션에 콘솔 앱 추가

클래스 라이브러리를 사용하는 콘솔 애플리케이션을 추가합니다. 이 앱은 사용자에게 문자열을 입력하라는 메시지를 표시하고 문자열이 대문자로 시작하는지 여부를 보고합니다.

  1. 터미널에서 다음 명령을 실행하여 콘솔 앱 프로젝트를 만듭니다.

    dotnet new console -f net6.0 -o ShowCase
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    The template "Console Application" was created successfully.
    Processing post-creation actions...
    Running 'dotnet restore' on ShowCase\ShowCase.csproj...
      Determining projects to restore...
      Restored C:\Projects\ClassLibraryProjects\ShowCase\ShowCase.csproj (in 210 ms).
    Restore succeeded.
    
  2. 다음 명령을 실행하여 솔루션에 콘솔 앱 프로젝트를 추가합니다.

    dotnet sln add ShowCase/ShowCase.csproj
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Project `ShowCase\ShowCase.csproj` added to the solution.
    
  3. ShowCase/Program.cs를 열고 모든 코드를 다음 코드로 바꿉니다.

    using UtilityLibraries;
    
    class Program
    {
        static void Main(string[] args)
        {
            int row = 0;
    
            do
            {
                if (row == 0 || row >= 25)
                    ResetConsole();
    
                string? input = Console.ReadLine();
                if (string.IsNullOrEmpty(input)) break;
                Console.WriteLine($"Input: {input}");
                Console.WriteLine("Begins with uppercase? " +
                     $"{(input.StartsWithUpper() ? "Yes" : "No")}");
                Console.WriteLine();
                row += 4;
            } while (true);
            return;
    
            // Declare a ResetConsole local method
            void ResetConsole()
            {
                if (row > 0)
                {
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
                Console.Clear();
                Console.WriteLine($"{Environment.NewLine}Press <Enter> only to exit; otherwise, enter a string and press <Enter>:{Environment.NewLine}");
                row = 3;
            }
        }
    }
    

    코드는 row 변수를 사용하여 콘솔 창에 기록된 데이터 행 수를 유지합니다. 25보다 크거나 같으면 코드는 콘솔 창을 지우고 사용자에게 메시지를 표시합니다.

    프로그램에서 문자열을 입력하라는 메시지를 사용자에게 표시합니다. 문자열이 대문자로 시작하는지 여부를 나타냅니다. 사용자가 문자열을 입력하지 않고 Enter 키를 누르면 애플리케이션이 종료되고 콘솔 창이 닫힙니다.

  4. 변경 내용을 저장합니다.

프로젝트 참조 추가

처음에는 새 콘솔 앱 프로젝트가 클래스 라이브러리에 액세스할 수 없습니다. 클래스 라이브러리의 메서드를 호출할 수 있도록 허용하려면 클래스 라이브러리 프로젝트에 대한 프로젝트 참조를 만듭니다.

  1. 다음 명령을 실행합니다.

    dotnet add ShowCase/ShowCase.csproj reference StringLibrary/StringLibrary.csproj
    

    터미널 출력은 다음 예제처럼 표시됩니다.

    Reference `..\StringLibrary\StringLibrary.csproj` added to the project.
    

앱 실행

  1. 터미널에서 다음 명령을 실행합니다.

    dotnet run --project ShowCase/ShowCase.csproj
    
  2. 문자열을 입력하고 Enter 키를 눌러 프로그램을 사용해 본 다음 Enter 키를 눌러 끝냅니다.

    터미널 출력은 다음 예제처럼 표시됩니다.

    Press <Enter> only to exit; otherwise, enter a string and press <Enter>:
    
    A string that starts with an uppercase letter
    Input: A string that starts with an uppercase letter
    Begins with uppercase? : Yes
    
    a string that starts with a lowercase letter
    Input: a string that starts with a lowercase letter
    Begins with uppercase? : No
    

추가 자료

다음 단계

이 자습서에서는 솔루션을 만들고, 라이브러리 프로젝트를 추가하고, 라이브러리를 사용하는 콘솔 앱 프로젝트를 추가했습니다. 다음 자습서에서는 솔루션에 단위 테스트 프로젝트를 추가합니다.