{"id":1047,"date":"2016-05-14T18:15:20","date_gmt":"2016-05-14T22:15:20","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=1047"},"modified":"2024-03-02T17:10:59","modified_gmt":"2024-03-02T22:10:59","slug":"c-6-features-string-interpolation","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2016\/05\/14\/c-6-features-string-interpolation\/","title":{"rendered":"C# 6 Features &#8211; String Interpolation"},"content":{"rendered":"<p>For this post in my series, I want to look at one of my favorite little time savers, String Interpolation.  String Interpolation is basically when you put tokens into a string and those are replaced for you by the language.  Previously, in C#, we might do one of the following things:<\/p>\n<pre>\r\n\/\/ Concatenation\r\nvar name = \"Pete\";\r\nvar greeting = \"Hello, \" + name;\r\n<\/pre>\n<pre>\r\n\/\/ String Format\r\nvar name = \"Pete\";\r\nvar greeting = string.Format(\"Hello, {0}\", name);\r\n<\/pre>\n<p>Sometimes, with a long string with a lot of tokens, this can get kind of unwieldy.  And, if you reuse tokens (say, a line break, or the person&#8217;s name), it can be almost impossible to keep things straight.  Other languages have had features like this for awhile.<\/p>\n<p>Swift:<\/p>\n<pre>\r\nvar name = \"Pete\"\r\nvar greeting = \"Hello, \\(name)\"\r\n<\/pre>\n<p>Ruby:<\/p>\n<pre>\r\nname = \"Pete\"\r\ngreeting = \"Hello #{name}\"\r\n<\/pre>\n<p>Now, in C# 6, we get that kind of clarity as well.  The syntax calls for you to use a dollar sign ($) in front of the string and then use curly braces surrounding the variable reference in the string.  Here is an example<\/p>\n<p>New C# 6 String Interpolation:<\/p>\n<pre>\r\nvar name = \"Pete\";\r\nvar greeting = $\"Hello, {name}\";\r\n<\/pre>\n<p>In my opinion, that beats the pants off of string.Format().  Additionally, you can also reference class properties by the same syntax.  Here is another example:<\/p>\n<pre>\r\nvar name = \"Pete\";\r\nvar greeting = $\"Hello, {name}.  Your name has {name.Length} letters.\";\r\n<\/pre>\n<p>You can also express values from methods:<\/p>\n<pre>\r\nvar items = new []{\"a\", \"b\", \"c\"};\r\nConsole.WriteLine($\"Items has {items.Count()} items\");\r\n<\/pre>\n<p>So, there you go.  I can promise you that I&#8217;m going to use this instead of string.Format() in every project that is running the necessary framework versions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For this post in my series, I want to look at one of my favorite little time savers, String Interpolation. String Interpolation is basically when you put tokens into a string and those are replaced &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[138],"class_list":["post-1047","post","type-post","status-publish","format-standard","hentry","category-c-6","tag-c-6"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/1047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=1047"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/1047\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=1047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=1047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=1047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}