Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Nuevas expresiones de tipo objetivo y matrices implícitas

Puedo escribir lo siguiente para inicializar un List<KeyValuePair<string, string>> en un inicializador de campo:

 List<KeyValuePair<string, string>> lst = new() { new("Key1", "Value1"), new("Key2", "Value2") }

aprovechando las new expresiones de tipo objetivo para las estructuras List<> contenedoras y KeyValuePair<> individuales.

¿Es posible inicializar de manera similar una matriz? Las new expresiones de tipo objetivo no se pueden usar para la matriz en sí, porque se debe inicializar una matriz.

Pero lo siguiente, usando matrices implícitamente tipadas, no se compila:

 KeyValuePair<string, string>[] arr = new [] { new("Key1", "Value1"), new("Key2", "Value2") }

con:

CS0826 No se encontró el mejor tipo para la matriz tipificada implícitamente

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede eliminar la parte new[] :

 using System.Collections.Generic; KeyValuePair<string, string>[] arr = { new("Key1", "Value1"), new("Key2", "Value2") };

Sin embargo, eso solo es válido como parte de una declaración de variable, que permite un array_initializer como el valor inicial, sin la necesidad de un array_creation_expression . Las declaraciones de variables incluyen campos, por supuesto:

 public class Test { private readonly KeyValuePair<string, string>[] arr = { new("Key1", "Value1"), new("Key2", "Value2") }; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!