0

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;
 }
asked Nov 12, 2016 at 8:54
6
  • 2
    You are not catching writeStrings return Commented Nov 12, 2016 at 8:56
  • @LPK - while completely correct, I think you misunderstood OP's intention - clearly writeString should write string - meaning it should be writing to console instead of returning result... Commented Nov 12, 2016 at 8:57
  • Use the Value returned by the method writeString Commented Nov 12, 2016 at 8:58
  • Console.WriteLine(writestring(ar)); Commented Nov 12, 2016 at 9:01
  • Do consider String.Join suggested by multiple answers, if you are trying to convert whole array into single string separated by a delimiter, though your question doesn't suggest that as a requirement. Commented Nov 12, 2016 at 9:15

4 Answers 4

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;
}
answered Nov 12, 2016 at 9:01

5 Comments

Additionally String.Join() can be used to replace the writeString() function with a few adjustments.
@PoomrokcThe3years In original question OP is not replicating Join functionality, since Join will add limiter post all the elements of the collection, as it creates a single string, here empty space is only appended i nthe beginning, not between elements
@MrinalKamboj What if I do output=" "+String.Join(String.Empty,ar); ? Would something like this work? I don't know i haven't code C# for like 3 years.
@PoomrokcThe3years would certainly, in fact that's what he's trying to do in current method, however, my gut feeling is OP might actually need a Join, that you have showed earlier :)
@SaurabhCooks I'm glad. Could you accept it is the correct answer :-)
2

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();
}
marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
answered Nov 12, 2016 at 9:08

10 Comments

Original functionality doesn't do the work of Join, eve if that's the intention as it just adds the empty space in the beginning and no where else
@MrinalKamboj Why not add the same here with " " + String.Join(delimitter, ar) ?
I think, he needs space between the array items and doing wrongly. In that case delimitter becomes string delimitter = " ";
That's your assumption of what OP needs, it is not specified anywhere, so I am not sure, what's the use case, he just asked about printing output, which was anyway wrong.
Please read his question once again. He asks for Also tips to make the code concise are most welcome
|
1

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));
answered Nov 12, 2016 at 9:07

Comments

1

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.

answered Nov 12, 2016 at 9:00

1 Comment

Please add some code to make it a valid answer or better use comment section for a suggestion

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.