Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / C#

const: odniesienie praktyczne

const: odniesienie praktyczne: const deklaruje wartość znaną przy kompilacji i wbudowywaną u konsumenta.

Wersja: C# language and .NET 10 reference runtime

Składnia

const /* grammar-specific form */

const deklaruje wartość znaną przy kompilacji i wbudowywaną u konsumenta.

Stosuj do prawdziwie stałych wartości prymitywnych i string.

Public const zmienione w bibliotece wymaga rekompilacji konsumentów; dla innych użyj static readonly.

Dozwolone typy i wyrażenia są ograniczone, a const reference może być tylko null lub string.

Kompletny program rzeczywiście używa const; oczekiwany stdout to dokładnie 7.

Przykłady

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);

Wynik

7

Kompletny program rzeczywiście używa `const`; oczekiwany stdout to dokładnie `7`.

Źródła