Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

416
Vistas
Error: cannot convert int to string while printing array values in Console.WriteLine

Code given below is giving error that at b[3] cannot convert from int to string

static void Main(string[] args)
    {
        int[] b = new int[5] { 1, 7, 8, 9, 2 };
        Console.WriteLine(b[3],b[4]); 
        
    }

whereas code given below is working properly without any issue

int[] b = new int[5] { 1, 7, 8, 9, 2 };
        Console.WriteLine($"{b[3]} {b[4]}");
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Console.WriteLine is not well suited to just print out passed multiple arguments (as print in python does, for example). Console.WriteLine overloads with 2 parameters (1, 2) require first parameter to be a string containing a composite format string and the second parameter will be used to fill placheholders in this format string.

If you want to print out several items you need either combine them into string manually. For example:

  • via string inteerpolation as in your question
    Console.WriteLine($"{b[3]} {b[4]}");
    
  • using string.Join (more convenient with collections):
    Console.WriteLine(string.Join(" ", new []{ b[3], b[4]}));
    

or use the format string:

Console.WriteLine("{0} {1}", b[3], b[4]);

or

Console.WriteLine("{0} {1}", new object[]{b[3], b[4]}); // better used with reference types
over 4 years ago · Santiago Trujillo Denunciar

0

Console.WriteLine expects one parameter to print. You are passing 2 parameters.

You can change to:

static void Main(string[] args)
{
    int[] b = new int[5] { 1, 7, 8, 9, 2 };
    Console.WriteLine(b[3]);
    Console.WriteLine(b[4]);
}

Or better:

static void Main(string[] args)
{
    int[] b = new int[5] { 1, 7, 8, 9, 2 };
    Console.WriteLine($"{b[3]} {b[4]}");
}

In this case I pass one parameter with string interpolation.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda