In this article, we are going to learn how to add parameters to a string in C# in a few different ways.

To download the source code for this article, you can visit our GitHub repository.

Let’s start

Using String.Format() Method

Firstly, we can add parameters to a string using the string.Format() method. With this method, we can insert objects, variables, or expressions at specific places in a formatted string:

var name = "John Doe";

Console.WriteLine(string.Format("Hi, my name is {0}", name));

The {0} within the format string is a format item. 0 is the index of the parameter that we insert at that position, in this case name. However, if the value that we want to insert is not a string, it is converted to string automatically.ย 

Let’s add another parameter called age which is an integer:

var name = "John Doe";

var age = 26;

Console.WriteLine(string.Format("Hi, my name is {0}. I am {1} years old", name, age));

As we can see, the compiler has no problems with this and in the runtime, we get our message:

Hi, my name is John Doe. I am 26 years old.

Using String.Replace() Method

The string.Replace() method has four overloads. But in our example, to add a parameter to a string, we are going to use an overload that has two string parameters:

public string Replace (string oldValue, string newValue);

The oldValue is the current string we want to replace, while newValue is the parameter we are adding:

var greeting = "Hi, my name is username";

Console.WriteLine(greeting.Replace("username", name));

string.Replace() method is case-sensitive. As such, if we were to pass in ‘Username’ as oldValue in the example above, no match will be found and the current greeting is returned.

Additionally, we can chain multiple string.Replace() to a string:

var age = 26;ย 

var greeting = "Hi, my name is username. I am age years old";

Console.WriteLine(greeting.Replace("username", name).Replace("age", age.ToString()));

When we run our application, both ‘username’ and ‘age’ will be replaced by name and age respectively. However, we had to convert age from int to string as this method only accepts string variables.

This method of adding parameters to a string is not used that often.

Using String Interpolation

String interpolation is available starting with C#6. Itย is a more readable method for adding parameters to a string. We use $ to identify a string literal as an interpolated string, and instead of using indexes, we use the variables themselves inside braces {} which is more practical and intuitive:

Console.WriteLine($"Hi, my name is {name}. I am {age} years old");

In addition, we can use the ternary conditional operator ?: to format strings when using string interpolation:

Console.WriteLine($"Hi, my name is {name}. I am {age} year{(age == 1 ? "" : "s")} old");

As you can see, we are either returning “year” if the age is equal to 1 or “years” if it is not.

Using StringBuilder.AppendFormat()

Lastly, it’s worth mentioning that if we’re using the StringBuilder class we can also add variables to a string by using StringBuilder.AppendFormat(). This method exists in the System.Text namespace. We can apply this method the same way we use string.Format():

var builder = new StringBuilder();

builder.AppendFormat("Hi, my name is {0}. I am {1} years old.", name, age);

Console.WriteLine(builder);

The resulting string is exactly the same as before.

We also need to mention one more thing. As of .NET 6, string interpolation is more memory-efficient and faster than both String.Format and StringBuilder.AppendFormat. This means, we should prefer it in all cases where the format string is known at compile time.

Conclusion

In this article, we have learned how to add parameters to a string in C# using various methods and examples. We can apply this concept when building applications to send dynamic messages and feedback to users.