Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / C#

System.ArgumentNullException: referência prática

System.ArgumentNullException: referência prática: ArgumentNullException especializa o erro de um argumento obrigatório que é null; ThrowIfNull conserva o nome da expressão.

Versão: .NET API; verified on .NET 10.0.14

Sintaxe

System.ArgumentNullException /* select a documented member or constructor */

ArgumentNullException especializa o erro de um argumento obrigatório que é null; ThrowIfNull conserva o nome da expressão.

Usa-a para impor referências obrigatórias imediatamente à entrada de um método.

Não captures a exceção apenas para continuar com um valor ausente; corrige o contrato do chamador.

Pode ser necessária uma conversão explícita com as sobrecargas object e void*; ParamName pode ser fornecido manualmente.

O programa completo usa realmente System.ArgumentNullException; o stdout exato esperado é input.

Exemplos

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

try { ArgumentNullException.ThrowIfNull((object?)null, "input"); } catch (ArgumentNullException error) { Console.WriteLine(error.ParamName); }

Resultado

input

O programa completo usa realmente `System.ArgumentNullException`; o stdout exato esperado é `input`.

Fontes