-4

I am making a rock, paper, scissors, lizard, Spock game. I already did it and it works perfectly! But there is a line of code that I've been using for two days now and I don't know the meaning of. Here it is:

{weapon2chose[bot_weapon_chosen]} 

I use it in order to take a random word from a string array and then print the word instead of just a number with the random class like this:

Console.WriteLine($"You fought against {weapon2chose[bot_weapon_chosen]} and lost...");

This is the random code I use as well:

string[] weapon2chose = {"rock", "paper", "scissors", "lizard", "Spock" }; 
Random and = new Random(); 
int bot_weapon_chosen = rnd.Next(weapon2chose.Length);

I'd like you to explain to me what the {} brackets are and how does it print out a string instead of a number.

JNevill
  • 46,980
  • 4
  • 38
  • 63
Ziolekx
  • 1
  • 1
  • 1
    It's called [string interpolation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated) – Arthur Rey Feb 24 '22 at 15:59

1 Answers1

1

This is called string interpolation. When a string constant begins with $, the curly braces are where you put replacement values.

For example:

int i = 55;
Console.WriteLine($"Value is {i}.");

The output is:

Value is 55.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466