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

415
Views
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 answers
Answer question

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 Report

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 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!