1
string[] groups;
int groupCount;
double[] grades;
int gradeCount;

So the groups and grades are in two separate arrays and I need to combine them as one string and add them to a new array.

string[] test = new string[groupCount];
for (int i = 0; i < groupCount; i++)
{
   test[i] = ("{0}:   {1}", groups[i], Math.Round(grades[i],2));              
   Console.WriteLine("{0}", test[i]);
}

How do i do it ?

2
  • Yeah it throws a lot of errors ; It expects ";" so it would be declared propperly; also : Error 1 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement Commented Oct 11, 2016 at 11:16
  • please have a look at how-to-ask Commented Oct 11, 2016 at 12:05

2 Answers 2

3

C# 6.0 string interpolation (please, notice $ before the string):

test[i] = $"{groups[i]}:   {Math.Round(grades[i],2)}"; 

Another possibility is Linq (in order to output the entire collection in one go):

string[] groups;
double[] grades;

...

var test = groups
  .Zip(grades, (group, grade) => $"{group}:    {Math.Round(grade, 2)}")
  .ToArray(); // array materialization (if you want just to output you don't need it)

Console.Write(String.Join(Environemnt.NewLine, test)); 
Sign up to request clarification or add additional context in comments.

1 Comment

Got my vote for interpolation. I always prefer answers with more up-to-date solutions. For more info, one can refer to: stackoverflow.com/documentation/c%23/24/c-sharp-6-0-features/49/…
2

You forgot string.Format()

It should be,

string.Format("{0}:   {1}", groups[i], Math.Round(grades[i], 2));

Hope helps,

1 Comment

OMG ! Lifesaver :D Thank you very much ! !!

Your Answer

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.