EN CSharp Operator Null Forgiving - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
Suppression of nullable warnings
Tells the nullable analysis that the expression is treated as non-null. Does not check the value and does not protect against NullReferenceException.
Simple example (Example.cs)
Save this example separately as Example.cs. Run it independently of the other examples on this page.
Example.cs:
// PROJECT-EXAMPLE
string? name = "ore";
System.Console.WriteLine(name!.Length);
Result:
3
Syntax
possiblyNull!.Member
Example 1
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)
{
string? text = "ore";
Console.WriteLine(text!.Length);
}
}
Example 2
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)
{
Dictionary<string, string> values = new() { ["name"] = "Archer" };
values.TryGetValue("name", out string? name);
Console.WriteLine(name!.ToUpperInvariant());
}
}
Official source: Microsoft Learn.