Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

explicit: practical reference

explicit: practical reference: explicit declares a conversion that requires an explicit cast.

Version: C# language; verified against C# 14 reference

Syntax

explicit /* grammar-specific form */

explicit declares a conversion that requires an explicit cast.

Use it for possible conversions that may lose data or fail.

The operator should throw a predictable exception and avoid hidden expensive work.

checked explicit may have a separate implementation; null behavior depends on operand types.

The complete program actually uses explicit; its exact expected stdout is 4.

Examples

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;

var value = (int)new Measure(4); Console.WriteLine(value);
readonly record struct Measure(int Value) { public static explicit operator int(Measure value) => value.Value; }

Result

4

The complete program actually uses `explicit`; its exact expected stdout is `4`.

Sources