I need some help with a couple of errors I'm receiving for C# in Visual Studio. They state cannot convert string to int or cannot convert int to char. Would someone be able to assist me with this, please?
Console.WriteLine(" The largest value in the array is ", numberArray, maximumIndex, "located at array index" maximumIndex);
//Loop through the two arrays and print the integer from integer array followed by the corresponding value from the string array
Console.WriteLine(" The numbers were: ");
for (int i = 0; i < numberArray.Length; i )
Console.WriteLine(numberArray[i], " is ", stringArray[i]); **Getting error for "numberArray"-Cannot convert 'int' to 'char; Getting error for "is"-Cannot convert string to int; Getting error for "stringArray"-Cannot convert string to int**
CodePudding user response:
C# approach
Though the (,) operator may be used in python or in some other languages, the (,) operator is not really a C# syntax within the Console.[method] arguments.
Solutions
You may use any of these approaches, it's all about preference. I will rank them from my favorite approach to the other possible approaches:
Console.WriteLine($"{numberArray[i]} is {stringArray[i]}");
// Example output: 1 is 2
That's the cleanest solution in my opinion. But another would be the use of Concatination
Console.WriteLine(numberArray[i] " is " stringArray[i]);
// Example output: 1 is 2
Hope this helped. More ways of approaching this topic exist but these are the two worth telling you about at the moment, can't think of any more comfortable version but I'm sure they exist.
Before I end this answer, I would just like to point out that you are missing brackets within the for loop. I'm not sure if C# allows that since I've never really been a big fan of none C syntax but incase you get another error, try adding brackets cause I'm assuming that issue will pop up sooner or later.
CodePudding user response:
you need to do in the console.writeline not a ,
CodePudding user response:
I modified your code as below;
public static void Main(string[] args)
{
int[] numberArray = {1, 5, 10};
var maximumIndex = 2;
Console.WriteLine("The largest value in the array is {0} {1} located at array index{2}", string.Join(",", numberArray), maximumIndex, maximumIndex);
}
Output: The largest value in the array is 1,5,10 2 located at array index2
refer Interpolated String