Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

const: practical reference

const: practical reference: const declares a compile-time value that is embedded into consuming code.

Version: C# language and .NET 10 reference runtime

Syntax

const /* grammar-specific form */

const declares a compile-time value that is embedded into consuming code.

Use it for truly constant primitive and string values.

Changing a public const in a library requires recompiling consumers; otherwise use static readonly.

Allowed types and expressions are limited, and a const reference can only be null or string.

The complete program actually uses const; its exact expected stdout is 7.

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;

const int Days = 7; Console.WriteLine(Days);

Result

7

The complete program actually uses `const`; its exact expected stdout is `7`.

Sources