{"id":9576,"date":"2022-10-11T20:08:57","date_gmt":"2022-10-11T20:08:57","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9576"},"modified":"2022-10-11T20:08:57","modified_gmt":"2022-10-11T20:08:57","slug":"alignment-using-string-interpolation-in-c","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/alignment-using-string-interpolation-in-c\/","title":{"rendered":"Alignment using string interpolation in C#"},"content":{"rendered":"<p>C#&#8217;s string interpolation supports alignment and format options<\/p>\n<p><strong>Alignment<\/strong><\/p>\n<p>Using the following syntax, <em>{expression, alignment}<\/em>, we can use negative alignment numbers for left-aligned expressions and positive numbers for right-aligned alignment. For example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar number = 123;\r\nConsole.WriteLine($&quot;|{number,-10}|&quot;);\r\nConsole.WriteLine($&quot;|{number,10}|&quot;);\r\n<\/pre>\n<p>This would produce the following <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n|123       |\r\n|       123|\r\n<\/pre>\n<p>See also <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/composite-formatting#alignment-component\" rel=\"noopener\" target=\"_blank\">Alignment component<\/a>.<\/p>\n<p><strong>Format String<\/strong><\/p>\n<p>We can use the following <em>{expression, alignment:format}<\/em> or <em>{expression:format}<\/em> syntax to add formatting, i.e.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar number = 123;\r\nConsole.WriteLine($&quot;|{number, -10:F3}|&quot;);\r\nConsole.WriteLine($&quot;|{number, 10:F3}|&quot;);\r\n<\/pre>\n<p>would produce the following <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n|123.000   |\r\n|   123.000|\r\n<\/pre>\n<p>and<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar number = 123;\r\nConsole.WriteLine($&quot;|{number:F3}|&quot;);\r\nConsole.WriteLine($&quot;|{number:F3}|&quot;);\r\n<\/pre>\n<p>would produce<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n|123.000|\r\n|123.000|\r\n<\/pre>\n<p>See also <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/composite-formatting#format-string-component\" rel=\"noopener\" target=\"_blank\">Format string component<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C#&#8217;s string interpolation supports alignment and format options Alignment Using the following syntax, {expression, alignment}, we can use negative alignment numbers for left-aligned expressions and positive numbers for right-aligned alignment. For example var number = 123; Console.WriteLine($&quot;|{number,-10}|&quot;); Console.WriteLine($&quot;|{number,10}|&quot;); This would produce the following |123 | | 123| See also Alignment component. Format String We can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-9576","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9576"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9576\/revisions"}],"predecessor-version":[{"id":9578,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9576\/revisions\/9578"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}