String Interpolation in C#
ฝัง
- เผยแพร่เมื่อ 14 พ.ย. 2024
- How do you use string interpolation in C#? This video shows you how to convert string.Format into string interpolation introduced in C# 6.0.
Code available here: github.com/fek...
Follow me on Twitter: @fekberg / fekberg
Check out my courses on Pluralsight: app.pluralsigh...
Music by: Spudmash Media check them out at www.spudmash.com/ and follow on Twitter / spudmashmedia
Font size will definitely be improved in upcoming videos to make it easier to watch on mobile!
Thank you it's very helpful
So, if I'm understanding this correctly, we can say the $ is an overloaded operator shorthand way of using C#'s string.Format() function.
Correct!
@@FilipEkberg I kept wondering if this existed. I was like "I know Python and JS have this, there must be some way C# does this kind of thing." I think most of the web apps we support where I work are on C# 5, so I'll still have to use string.Format there for the time being.
Hey filip! i also used interpolation in my application. Apart from that i have a query , if you could help me out in that!.
I have a scenario:
I have a session variable named as Session["regno"] = "xyz-123" (and the values can be changed). which is further a part of SMS Text to be broadcasted to end user.
"Dear User, your reg No is Session["regno"].toString()"
To avoid the hard code approach i have stored the above text into a database table and calling back the text using stored procedure and further storing it in a datatable like below:
SMSContent = dtSMSTemplateContent.Rows[0][0].ToString(); //dtSMSTemplateContent is returning the tuple which is a correct manner.
THE PROBLEM IS:
When i am trying to send the text statement; it is:
*Dear User, your reg No is Session["regno"].toString()*
rather than it should be
*Dear User, your reg No is xyz-123*
Hi, you could use simple string formatting.
Change the content in the database to "Dear User, your reg No is {0}" and when you construct the string in your application you do the following:
var content = string.Format(SMSContent, Session["regno"].toString());
Hope this helps