I have written a simple code and have passed an array to the method and have returned the value and trying to get it printed. Somehow, I am not getting the required output. Please point out where I got wrong. Also tips to make the code concise are most welcome.
static void Main(string[] args)
{
string[] ar = new string[] { "hello", "world", "people" };
writeString(ar);
Console.WriteLine(ar);
Console.ReadLine();
}
static string writeString(string[] ar)
{
string a1 = " ";
foreach (string a in ar)
{
a1 = a1 + a;
}
return a1;
}
4 Answers 4
I hope the code sample makes it clear. You have to capture the return value from the writeString function and output that.
static void Main(string[] args)
{
string[] ar = new string[] { "hello", "world", "people" };
string output = writeString(ar);
Console.WriteLine(output);
Console.ReadLine();
}
static string writeString(string[] ar)
{
string a1 = " ";
foreach (string a in ar)
{
a1 = a1 + a;
}
return a1;
}
5 Comments
You can refactor
it as below with concise code without the need of another method.
Note: I have added space before the array items as you did. But if you need space between each items of array then change the delimiter
line as string delimiter = " ";
You can change the delimiter
below with any character like ,
etc.
static void Main(string[] args)
{
string[] ar = new string[] { "hello", "world", "people" };
string delimiter = "";
string output = " " + String.Join(delimiter, ar);
Console.WriteLine(output);
// You can do the same in one line as below by commenting above three lines
// Console.WriteLine(" " + String.Join("", ar));
Console.ReadLine();
}
10 Comments
Join
, eve if that's the intention as it just adds the empty space in the beginning and no where else" " + String.Join(delimitter, ar)
?delimitter
becomes string delimitter = " ";
Also tips to make the code concise are most welcome
I have written a simple code and have passed an array to the method and have returned the value and trying to get it printed. Somehow, I am not getting the required output
As everyone has pointed that you are not receiving the returned value to print it or do anything with it, so a simple code modification is first receive the result / output:
var result = writeString(ar);
What is your Result expectation
Since you are printing the array, instead of returned value. Array is not even modified in the called function anywhere, therefore it remains exactly same. Simplest way to achieve the expected result would be:
Console.WriteLine(writeString(ar));
Comments
You are not printing the string returned by the writeString(string[] ar)
method. Instead you are printing the input string array variable ar
(on line Console.WriteLine(ar)
). That's your problem. That WriteLine
statement prints only the fully qualified name of the variable ar
's type: System.String[]
.
Change the line Console.WriteLine(ar)
to Console.WriteLine(writeString(ar))
to get your desired output.
writeString
s returnwriteString
should write string - meaning it should be writing to console instead of returning result...writeString
Console.WriteLine(writestring(ar));