Upgrade to Pro — share decks privately, control downloads, hide ads and more …

New Features in C# 10/11

New Features in C# 10/11

2022/5/14
TechFeed Conference 2022

New Features in C# 10/11
C# 10 および C# 11 (Preview) の主な新機能について

Akira Inoue

May 14, 2022
Tweet

More Decks by Akira Inoue

Other Decks in Technology

Transcript

  1. Senior Cloud Solution Architect, Microsoft Japan
    井上 章 (いのうえ あきら) @chack411
    2022/5/14
    New Features in C# 10/11

    View Slide

  2. View Slide

  3. Simplify
    your code with C# 10

    View Slide

  4. global using ディレクティブ
    using ディレクティブ - C# リファレンス | Microsoft Docs
    // ex: HomeController.cs
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Logging;
    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using static System.Console;
    using Env = System.Environment;
    namespace WebApplication1.Controllers
    {
    ...
    }
    通常の using ディレクティブ (ファイル スコープ)
    // ex: globalusings.cs
    global using Microsoft.AspNetCore.Mvc;
    global using Microsoft.Extensions.Logging;
    global using System;
    global using System.Diagnostics;
    global using System.Linq;
    global using System.Threading.Tasks;
    global using static System.Console;
    global using Env = System.Environment;
    global using ディレクティブ (プロジェクト スコープ)
    // ex: HomeController.cs
    namespace WebApplication1.Controllers
    {
    ...
    }

    View Slide

  5. 暗黙的な global using ディレクティブ
    次の SDK を使⽤するプロジェクトには、暗黙的な global using ディレクティブが追加される
    SDK 既定の名前空間
    Microsoft.NET.Sdk
    System
    System.Collections.Generic
    System.IO
    System.Linq
    System.Net.Http
    System.Threading
    System.Threading.Tasks
    Microsoft.NET.Sdk.Web
    System.Net.Http.Json
    Microsoft.AspNetCore.Builder
    Microsoft.AspNetCore.Hosting
    Microsoft.AspNetCore.Http
    Microsoft.AspNetCore.Routing
    Microsoft.Extensions.Configuration
    Microsoft.Extensions.DependencyInjection
    Microsoft.Extensions.Hosting
    Microsoft.Extensions.Logging
    Microsoft.NET.Sdk.Worker
    Microsoft.Extensions.Configuration
    Microsoft.Extensions.DependencyInjection
    Microsoft.Extensions.Hosting
    Microsoft.Extensions.Logging
    Microsoft.NET.Sdk.WindowsDesktop (Windows Forms)
    Microsoft.NET.Sdk 名前空間
    System.Drawing
    System.Windows.Forms
    Microsoft.NET.Sdk.WindowsDesktop (WPF) Microsoft.NET.Sdk 名前空間
    .NET プロジェクト SDK の概要 | Microsoft Docs

    View Slide

  6. 最上位レベル ステートメントによる Program.cs の簡素化
    using System;
    namespace ConsoleApp2
    {
    internal class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Hello World!");
    }
    }
    }
    Console.WriteLine("Hello World!");
    Program.cs (Console app in .NET 5) Program.cs (Console app in .NET 6, C# 10)
    .NET 6 での C# コンソール アプリ テンプレートの変更 - .NET | Microsoft Docs
    最上位レベルのステートメント - Main メソッドを使⽤しないプログラム | Microsoft Docs

    View Slide

  7. ファイル スコープ名前空間 宣⾔
    namespace SampleNamespace
    {
    class SampleClass { }
    interface ISampleInterface { }
    struct SampleStruct { }
    enum SampleEnum { a, b }
    delegate void SampleDelegate(int i);
    }
    namespace SampleNamespace;
    class SampleClass { }
    interface ISampleInterface { }
    struct SampleStruct { }
    enum SampleEnum { a, b }
    delegate void SampleDelegate(int i);
    通常の名前空間宣⾔ ファイルスコープ名前空間宣⾔ (C# 10)
    名前空間キーワード - C# リファレンス | Microsoft Docs

    View Slide

  8. New Features in
    C# 11 Preview

    View Slide

  9. C# 11 プレビュー機能
    • .NET 6.0.200 SDK または Visual Studio 2022 バージョン 17.1 以降で利⽤可能
    • コンパイラ オプション (.csproj) の LangVersion を preview に設定


    Exe
    net6.0
    enable
    enable
    preview


    ※ C# 11 は .NET 7 (2022年11⽉) での正式リリースを予定
    C# 11 の新機能 - C# ガイド | Microsoft Docs

    View Slide

  10. 未加⼯の⽂字列リテラル / ⽂字列補間の改⾏
    const int veryCold = -30;
    const int comfortable = 20;
    string jsonString =
    $$"""
    {
    "TemperatureRanges": {
    "Cold": {
    "High": {{comfortable}},
    "Low": {{veryCold}}
    }
    }
    }
    """;
    未加⼯の⽂字列リテラルと⽂字列補間
    string message = $"The usage policy for {safetyScore} is {
    safetyScore switch
    {
    > 90 => "Unlimited usage",
    > 80 => "General usage, with daily safety check",
    > 70 => "Issues must be addressed within 1 week",
    > 50 => "Issues must be addressed within 1 day",
    _ => "Issues must be addressed before continued use",
    }
    }";
    ⽂字列補間の式と改⾏
    $ - ⽂字列補間 - C# リファレンス | Microsoft Docs

    View Slide

  11. UTF-8 ⽂字列リテラル (proposal)
    byte[] array = "hello"; // new byte[] { 0x68, 0x65, 0x6c, 0x6c, 0x6f }
    Span span = "dog"; // new byte[] { 0x64, 0x6f, 0x67 }
    ReadOnlySpan span = "cat"; // new byte[] { 0x63, 0x61, 0x74 }
    // u8 suffix on string literals
    string s1 = "hello"u8; // Error
    var s2 = "hello"u8; // OK and type is byte[]
    Span s3 = "hello"u8; // OK due to an implicit user-defined conversion from byte[]
    ReadOnlySpan s3 = "hello"u8; // OK due to an implicit user-defined conversion from byte[]
    csharplang/utf8-string-literals.md at main · dotnet/csharplang · GitHub

    View Slide

  12. and more … !
    GitHub - dotnet/csharplang
    The official repo for the design of the C# programming language

    View Slide

  13. References
    • .NET SDK
    • https://dotnet.Microsoft.com/download
    • Visual Studio
    • https://visualstudio.microsoft.com/downloads
    • Learn more:
    • https://docs.microsoft.com/dotnet/csharp/whats-new/csharp-9
    • https://docs.microsoft.com/dotnet/csharp/whats-new/csharp-10
    • https://docs.microsoft.com/dotnet/csharp/whats-new/csharp-11
    • https://devblogs.microsoft.com/dotnet/welcome-to-csharp-10/
    • https://devblogs.microsoft.com/dotnet/early-peek-at-csharp-11-features/
    • https://devblogs.microsoft.com/dotnet/csharp-11-preview-updates/

    View Slide

  14. n 本書に記載した情報は、本書各項⽬に関する発⾏⽇現在の Microsoft の⾒解を表明するものです。Microsoftは絶えず変化する市場に対応しなければならないため、ここに記載した
    情報に対していかなる責務を負うものではなく、提⽰された情報の信憑性については保証できません。
    n 本書は情報提供のみを⽬的としています。 Microsoft は、明⽰的または暗⽰的を問わず、本書にいかなる保証も与えるものではありません。
    n すべての当該著作権法を遵守することはお客様の責務です。Microsoftの書⾯による明確な許可なく、本書の如何なる部分についても、転載や検索システムへの格納または挿⼊を⾏
    うことは、どのような形式または⼿段(電⼦的、機械的、複写、レコーディング、その他)、および⽬的であっても禁じられています。これらは著作権保護された権利を制限する
    ものではありません。
    n Microsoftは、本書の内容を保護する特許、特許出願書、商標、著作権、またはその他の知的財産権を保有する場合があります。Microsoftから書⾯によるライセンス契約が明確に
    供給される場合を除いて、本書の提供はこれらの特許、商標、著作権、またはその他の知的財産へのライセンスを与えるものではありません。
    n Microsoft, Windows, その他本⽂中に登場した各製品名は、Microsoft Corporation の⽶国およびその他の国における登録商標または商標です。
    その他、記載されている会社名および製品名は、⼀般に各社の商標です。

    View Slide