Please answer

Updated on July 12, 2017 in [C] Beginner
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on July 12, 2017

What is the use of interpolated string

  • Liked by
Reply
0 on July 12, 2017

Here You Go.

(that was hard, please refer to the Forum Etiquette post for guidelines on posting).

  • Liked by
Reply
Cancel
2 on July 12, 2017

It has few advantages:

  1. Readability – while machine compiling your code doesn’t care about this you as a human might find easier to read:
    string.Format("Please select {0} {1}", numerOfUnits, unitType)
    

    instead
    "Please select" + numerOfUnits.ToString() + " " + unitType.ToString()
    

    Especially when you have much more complex strings
  2. Reusability – you can have 1 string for the same message (with parameters) and re-use all over without risking that you would show different messages while you mean the same
  3. Auto conversion – string.Format uses ToString method, so you can use it with any type, along with those you have defined as long as you override ToString method of your class.
Devoted
on July 12, 2017

As of Unity 2017.1 which included C# 6 features you can now do string interpolation like this:

Debug.Log ($"Please select {numberOfUnits} {unitType}");

Helpful
on July 12, 2017

Cool – I learned something new 🙂 Thanks!

Show more replies
  • Liked by
Reply
Cancel