C# 6.0 String Interpolation

C# 6.0 String Interpolation

In C# 6, Microsoft has introduced a lot of new features for neat & clean codes. String Interpolation is also one of the feature introduced in C# 6. I have seen on many websites that IT professional is having a lot of misconception about it. I do not want to disclose the websites name but I have seen a lot of misinterpreted explanations for the C# 6 string interpolation.

Let me discuss some misconceptions about it.

Some IT professional discussing that in C# 5 it was written like:

  1. Console.WriteLine(String.Format("User details : UserId {0}, UserName {1}, EmailId {2}, ContactNumber {3}", user.UserId, user.UserName, user.EmailId,  user.ContactNumber));  
And now in C# 6
  1. Console.WriteLine("User details : UserId {0}, UserName {1}, EmailId {2}, ContactNumber {3}", user.UserId, user.UserName, user.EmailId, user.ContactNumber);  
And many more such type of misconception but I want to clarify that this not the fact.

Let ,me show you the String Formatting features available in C# 5.

User.cs
  1. class User  
  2. {  
  3.    public int UserId { getset; }  
  4.    public string UserName { getset; }  
  5.    public string EmailId { getset; }  
  6.    public string ContactNumber { getset; }  
  7. }  
Way 1:
  1. static void Main(string[] args)  
  2. {  
  3.    User user = new User { UserId = 1001, UserName = "BNarayan", EmailId = "[email protected]", ContactNumber = "99XXXXXXXX" };  
  4.   
  5.    Console.WriteLine("User details : UserId {0}, UserName {1}, EmailId {2}, ContactNumber {3}", user.UserId, user.UserName, user.EmailId, user.ContactNumber);  
  6. }  
Way 2:

 

  1. Console.WriteLine(String.Format("User details : UserId {0}, UserName {1}, EmailId {2}, ContactNumber {3}", user.UserId, user.UserName, user.EmailId,  user.ContactNumber));  
Way 3:
  1. string str1 = "User details : UserId " + user.UserId + ", UserName " + user.UserName + ", EmailId " + user.EmailId + ", ContactNumber " + user.ContactNumber;  
Way 4:
  1. string str2 = String.Format("User details : UserId {0}, UserName {1}, EmailId {2}, ContactNumber {3}", user.UserId, user.UserName, user.EmailId, user.ContactNumber);  
All these features are already available in C# 5. You may have noticed here one thing for Console.WriteLine and String.Format that I am able to pass any number of arguments with these 2 methods.

Yes, this is possible. If you can see the following screenshot it is clearly saying that second parameter of String.Format and Console.WriteLine is having params keyword which allows any number of arguments.

String.Format

Code

Then the question is that what problem is solved by string interpolation of C# 6. Suppose I want to write a line like the following:
  1. string str3 = "User details : UserId {user.UserId}, UserName {user.UserName}, EmailId {user.EmailId}, ContactNumber {user.ContactNumber}";  
But the output will be:

User details : UserId {user.UserId}, UserName {user.UserName}, EmailId {user.EmailId}, ContactNumber {user.ContactNumber}

Whereas I need output like:

User details: UserId 1001, UserName BNarayan, EmailId [email protected], ContactNumber 99XXXXXXXX.

I can use the String.Format or string concatenation to achieve this but both the methods are not so easy. In case of String.Format I need to pass a lot of arguments and remembering the sequence of the argument and in case of string concatenation I need to write a lot of double quotes and + signs.

But C# 6 resolves this using the feature which is known as string interpolation. In C# 6 we can write the following,
  1. string str4 = $"User details : UserId { user.UserId}, UserName { user.UserName}, EmailId { user.EmailId}, ContactNumber { user.ContactNumber}";  
or
  1. //in C# 5   
  2. string str5 = user.UserId + " " + user.UserName + " " + user.EmailId + " " + user.ContactNumber;  
  3. //in C# 6  
  4. string str6 = $ "{user.UserId} {user.UserName} {user.EmailId} {user.ContactNumber}";  
  5. //also in C# 6  
  6. private string GetUserDetails()  
  7. {  
  8.     return $ "{UserId} + {UserName} + {EmailId} + {ContactNumber}";  
  9. }  

 


Similar Articles