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]}");
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:
Console.WriteLine($"{b[3]} {b[4]}");
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
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.