Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / C#

struct: praktická referencia

struct: praktická referencia: struct deklaruje hodnotový typ, ktorý sa štandardne kopíruje hodnotou.

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

Syntax

struct /* grammar-specific form */

struct deklaruje hodnotový typ, ktorý sa štandardne kopíruje hodnotou.

Použite ju na malé hodnoty bez identity, ideálne readonly.

Veľká meniteľná struct spôsobuje drahé a prekvapivé kópie.

default nuluje polia bez vyjadrenia zámeru konštruktora; boxing vytvorí objekt.

Úplný program skutočne používa struct; presne očakávaný stdout je 5.

Príklady

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 point = new Point(2, 3); Console.WriteLine(point.X + point.Y); readonly struct Point { public Point(int x, int y) => (X, Y) = (x, y); public int X { get; } public int Y { get; } }

Výsledok

5

Úplný program skutočne používa `struct`; presne očakávaný stdout je `5`.

Zdroje