EloiStree / HelloSharpForUnity3D

Here we are going to check all the basic need to work in Unity3D with C#
0 stars 0 forks source link

Topic: use $"" #406

Open EloiStree opened 1 month ago

EloiStree commented 1 month ago

🤖 In C#, the $ symbol is used to define an interpolated string. An interpolated string allows you to embed expressions inside a string literal, which are evaluated and their results are included in the string.

Example 1: Basic String Interpolation

string name = "John";
int age = 30;
string greeting = $"Hello, my name is {name} and I am {age} years old.";

Here, the expressions {name} and {age} are evaluated and their values are inserted into the string. The result is:

// Output: "Hello, my name is John and I am 30 years old."

Example 2: Expressions in Interpolation

You can also use more complex expressions inside the interpolation braces:

int a = 5;
int b = 10;
string result = $"The sum of {a} and {b} is {a + b}.";

This will output:

// Output: "The sum of 5 and 10 is 15."

Example 3: Formatting Numbers

You can also format numbers within an interpolated string:

double price = 123.456;
string formattedPrice = $"The price is {price:C2}.";

This uses the :C2 format specifier to format the number as currency with two decimal places:

// Output: "The price is $123.46."

Example 4: Combining with Verbatim Strings

You can combine $ with @ to create an interpolated verbatim string, which is useful when dealing with multiline or file paths that also require interpolation:

string folder = "Documents";
string fileName = "file.txt";
string filePath = $@"C:\Users\YourName\{folder}\{fileName}";

This results in:

// Output: "C:\Users\YourName\Documents\file.txt"

These examples show how $ is used to create interpolated strings in C#.