Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / C#

struct: referência prática

struct: referência prática: struct declara um tipo de valor copiado por valor por predefinição.

Versão: C# language; verified against C# 14 reference

Sintaxe

struct /* grammar-specific form */

struct declara um tipo de valor copiado por valor por predefinição.

Usa-o para pequenos valores sem identidade, de preferência readonly.

Uma struct mutável grande causa cópias dispendiosas e surpreendentes.

default zera os campos sem exprimir a intenção do construtor; boxing cria um objeto.

O programa completo usa realmente struct; o stdout exato esperado é 5.

Exemplos

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

Resultado

5

O programa completo usa realmente `struct`; o stdout exato esperado é `5`.

Fontes