ES CSharp Operator Null Forgiving - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
Supresión de advertencias que aceptan valores NULL
Le indica al análisis que acepta valores NULL que la expresión se trata como no null. No comprueba el valor y no protege contra NullReferenceException.
Ejemplo sencillo (Example.cs)
Guarde este ejemplo por separado como Example.cs. Ejecútelo independientemente de los demás ejemplos de esta página.
Example.cs:
// PROJECT-EXAMPLE
string? name = "ore";
System.Console.WriteLine(name!.Length);
Resultado:
3
Sintaxis
possiblyNull!.Member
Ejemplo 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);
}
}
Ejemplo 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());
}
}
Fuente oficial: Microsoft Aprender.