nllatimo
nllatimo

Reputation: 138

using "{0} of {1}" in a ToString

Printing out a deck of cards I have a tostring in my class that reads

public override string ToString(){
  return GetCard() + " of " + GetSuit();}//end ToString

but I want all of the results to be in even columns so I tried

public override string ToString(){
  return "{0,5}of{1}",GetCard(),GetSuit();}//end ToString

and I don't think I can do that and I get an error. Is there another way to modify a tostring so I make even columns?

Upvotes: 0

Views: 94

Answers (2)

nllatimo
nllatimo

Reputation: 138

solved using

public override string ToString(){
return $"{GetCard(),-2} of {GetSuit()}";}//end ToString```

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186813

You, probably, are looking for string interpolation:

 public override string ToString() => $"{GetCard(),5} of {GetSuit}";

please, note the syntax: $ before the interpolated atring and values in braces "...{value}..."

Upvotes: 2

Related Questions