CKTIS / C#
checked: riferimento pratico
checked: riferimento pratico: checked abilita il controllo overflow per operazioni intere e conversioni incluse.
Versione: C# language and .NET 10 reference runtime
Sintassi
checked /* grammar-specific form */checked abilita il controllo overflow per operazioni intere e conversioni incluse.
Usalo ai confini finanziari, per dimensioni e dati non attendibili.
Non influenza allo stesso modo floating point o il codice nella funzione chiamata.
L’overflow genera OverflowException; le costanti possono essere controllate in compilazione.
Il programma completo usa realmente checked; lo stdout esatto atteso è overflow.
Esempi
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 { checked { var value = int.MaxValue; value++; } } catch (OverflowException) { Console.WriteLine("overflow"); }
Risultato
overflow
Il programma completo usa realmente `checked`; lo stdout esatto atteso è `overflow`.