본문 바로가기

워크/C# 5.0

새로운 라이브러리와 API

C# 5.0과 .NET Framework 4.5에는 다양한 새로운 라이브러리와 API가 도입되었습니다. 이 중 몇 가지 주요 기능에 대한 예시 코드를 아래에 설명하겠습니다:

 

HttpClient: .NET Framework 4.5에 도입된 HttpClient 클래스를 사용하여 웹 리소스에 액세스할 수 있습니다.

using System.Net.Http;

class Program
{
    static async Task Main(string[] args)
    {
        using (HttpClient client = new HttpClient())
        {
            string content = await client.GetStringAsync("https://www.example.com");
            Console.WriteLine(content);
        }
    }
}

 

Zip 압축/해제:

.NET 4.5에서는 System.IO.Compression 네임스페이스 아래에 ZipFile 및 ZipArchive 클래스가 추가되어 ZIP 압축 및 해제 기능을 쉽게 구현할 수 있습니다.

using System.IO.Compression;

class Program
{
    static void Main(string[] args)
    {
        string startPath = @"c:\example\start";
        string zipPath = @"c:\example\result.zip";
        string extractPath = @"c:\example\extract";

        ZipFile.CreateFromDirectory(startPath, zipPath);   // Create a ZIP
        ZipFile.ExtractToDirectory(zipPath, extractPath);  // Extract from a ZIP
    }
}

Path 클래스의 추가 메서드: Path 클래스에는 파일 및 디렉터리 경로를 조작하는 데 유용한 몇 가지 새로운 메서드가 추가되었습니다.

using System.IO;

class Program
{
    static void Main(string[] args)
    {
        var tempFile = Path.GetTempFileName();  // Get a uniquely named, zero-byte temporary file.
        var randomFileName = Path.GetRandomFileName();  // Returns a random folder name or file name.
    }
}

 

Caller Info Attributes: 이는 디버깅 및 진단에 유용하게 사용될 수 있습니다. 이미 이전의 예시에서 다루었습니다.

 

async 및 await: 이는 비동기 프로그래밍을 위한 주요 기능입니다. 이에 대한 예시는 앞서 이미 제공하였습니다.

 

이러한 새로운 라이브러리 및 API를 사용함으로써 개발자는 보다 효율적이고 직관적인 방식으로 다양한 작업을 수행할 수 있게 되었습니다.

 

 

 

'워크 > C# 5.0' 카테고리의 다른 글

활용 사례: 비동기 패턴 응용  (0) 2023.09.03
성능 최적화  (0) 2023.09.01
dynamic 키워드의 향상  (0) 2023.08.31
속성 강화  (0) 2023.08.31
Windows Runtime (WinRT) 지원  (0) 2023.08.30