Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / C#

new: referencia práctica

new: referencia práctica: new crea un objeto, array o delegate, u oculta un miembro heredado según el contexto.

Versión: C# language; verified against C# 14 reference

Sintaxis

new /* grammar-specific form */

new crea un objeto, array o delegate, u oculta un miembro heredado según el contexto.

Úsalo para construir explícitamente tras validar los datos requeridos.

Ocultar con new no es polimorfismo y depende del tipo estático de la referencia.

Un constructor puede lanzar una excepción; target-typed new necesita un tipo contextual inequívoco.

El programa completo usa realmente new; su stdout esperado exacto es 2.

Ejemplos

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 value = new List<int> { 1, 2 }; Console.WriteLine(value.Count);

Resultado

2

El programa completo usa realmente `new`; su stdout esperado exacto es `2`.

Fuentes