Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

default: practical reference

default: practical reference: default creates a type’s default value or the default switch label depending on context.

Version: C# language and .NET 10 reference runtime

Syntax

default /* grammar-specific form */

default creates a type’s default value or the default switch label depending on context.

Use it explicitly when zero or null is a valid initial state or fallback.

default produces the zero/default value and does not invoke an explicitly declared parameterless instance constructor of a struct; that zeroed state can violate a domain invariant.

For a reference it is null, for nullable HasValue=false, and for a struct every field has default.

The complete program actually uses default; its exact expected stdout is 0.

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;

int value = default; Console.WriteLine(value);

Result

0

The complete program actually uses `default`; its exact expected stdout is `0`.

Sources