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
{
...
}
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
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
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
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
11. UTF-8 ⽂字列リテラル (proposal)
byte[] array = "hello"; // new byte[] { 0x68, 0x65, 0x6c, 0x6c, 0x6f }
Span<byte> span = "dog"; // new byte[] { 0x64, 0x6f, 0x67 }
ReadOnlySpan<byte> 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<byte> s3 = "hello"u8; // OK due to an implicit user-defined conversion from byte[]
ReadOnlySpan<byte> s3 = "hello"u8; // OK due to an implicit user-defined conversion from byte[]
csharplang/utf8-string-literals.md at main · dotnet/csharplang · GitHub
12. and more … !
GitHub - dotnet/csharplang
The official repo for the design of the C# programming language
14. n 本書に記載した情報は、本書各項⽬に関する発⾏⽇現在の Microsoft の⾒解を表明するものです。Microsoftは絶えず変化する市場に対応しなければならないため、ここに記載した
情報に対していかなる責務を負うものではなく、提⽰された情報の信憑性については保証できません。
n 本書は情報提供のみを⽬的としています。 Microsoft は、明⽰的または暗⽰的を問わず、本書にいかなる保証も与えるものではありません。
n すべての当該著作権法を遵守することはお客様の責務です。Microsoftの書⾯による明確な許可なく、本書の如何なる部分についても、転載や検索システムへの格納または挿⼊を⾏
うことは、どのような形式または⼿段(電⼦的、機械的、複写、レコーディング、その他)、および⽬的であっても禁じられています。これらは著作権保護された権利を制限する
ものではありません。
n Microsoftは、本書の内容を保護する特許、特許出願書、商標、著作権、またはその他の知的財産権を保有する場合があります。Microsoftから書⾯によるライセンス契約が明確に
供給される場合を除いて、本書の提供はこれらの特許、商標、著作権、またはその他の知的財産へのライセンスを与えるものではありません。
n Microsoft, Windows, その他本⽂中に登場した各製品名は、Microsoft Corporation の⽶国およびその他の国における登録商標または商標です。
その他、記載されている会社名および製品名は、⼀般に各社の商標です。