Repository navigation
ZH TW CSharp Keyword notnull
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
類型的通用限制必須不可為空。违规行为是通过可空分析来评估的,而不是自动运行时排除。
請將此範例另存為 Example.cs,並與本頁其他範例分開執行。
Example.cs:
// PROJECT-EXAMPLE
T Echo<T>(T value) where T : notnull => value;
System.Console.WriteLine(Echo("ore"));結果:
ore
where T : notnullusing System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine(Values.Identity("ore"));
}
}
public static class Values { public static T Identity<T>(T value) where T : notnull => value; }using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
Storage<int> storage = new();
storage.Put(1, "ore");
Console.WriteLine(storage.Get(1));
}
}
public class Storage<TKey> where TKey : notnull { private readonly Dictionary<TKey, string> values = new(); public void Put(TKey key, string value) { values[key] = value; } public string Get(TKey key) => values[key]; }官方來源:Microsoft學習。
ClassicUO — Bad Newbie & C# Scripts IDE