A new way to format strings in C# 6
February 22, 2016 Leave a comment
In C# 5 we often format strings with the string.Format function. It requires a format string and then a number of parameters that are passed into the format string. C# 6 has a nice addition which simplifies that syntax.
Let’s start with the following Rockband object:
public class Rockband
{
public string Name { get; }
public int NumberOfMembers { get; }
public decimal ConcertFee { get; }
public Rockband(string name, int numberOfMembers, decimal concertFee)
{
Name = name;
NumberOfMembers = numberOfMembers;
ConcertFee = concertFee;
}
public string GetFancyName()
{
return Name.ToUpper();
}
}
The fancy name is not too fancy but that’s not important now.