Text Templates in C#

C# offers built-in support for creating dynamic content or showing customized output to the user with the System.Text.StringBuilder class and string interpolation. While not as feature-rich as Go’s templating system, these tools provide similar functionality for basic text manipulation.

using System;
using System.Collections.Generic;
using System.Text;

class Program
{
    static void Main()
    {
        // We can create a new StringBuilder and append text to it.
        // This is similar to creating and parsing a template.
        StringBuilder sb = new StringBuilder();
        sb.Append("Value is {0}\n");

        // We can use string.Format to replace placeholders with values,
        // similar to executing a template.
        Console.Write(string.Format(sb.ToString(), "some text"));
        Console.Write(string.Format(sb.ToString(), 5));
        Console.Write(string.Format(sb.ToString(), 
            new string[] { "C#", "F#", "VB.NET", "C++" }));

        // Helper function we'll use below.
        Func<string, string> Create = (t) => t;

        // If the data is an object, we can use string interpolation
        // to access its properties.
        string t2 = Create("Name: {0}\n");

        Console.Write(string.Format(t2, new { Name = "Jane Doe" }.Name));

        // The same applies to dictionaries.
        var dict = new Dictionary<string, string> { { "Name", "Mickey Mouse" } };
        Console.Write(string.Format(t2, dict["Name"]));

        // Conditional execution can be achieved using the ternary operator
        // in string interpolation.
        string t3 = Create("{0}\n");
        Console.Write(string.Format(t3, !string.IsNullOrEmpty("not empty") ? "yes" : "no"));
        Console.Write(string.Format(t3, !string.IsNullOrEmpty("") ? "yes" : "no"));

        // We can use string.Join to concatenate elements of an array,
        // similar to ranging over a slice in a template.
        string t4 = Create("Range: {0}\n");
        Console.Write(string.Format(t4, 
            string.Join(" ", new string[] { "C#", "F#", "VB.NET", "C++" })));
    }
}

To run the program, save it as TextTemplates.cs and use the C# compiler:

$ csc TextTemplates.cs
$ mono TextTemplates.exe
Value is some text
Value is 5
Value is System.String[]
Name: Jane Doe
Name: Mickey Mouse
yes
no
Range: C# F# VB.NET C++

This C# code demonstrates similar concepts to the original Go code, using C#’s string manipulation and formatting capabilities. While C# doesn’t have a built-in templating system like Go’s text/template, the StringBuilder class and string interpolation provide similar functionality for basic text manipulation tasks.

The string.Format method is used to replace placeholders in the strings, similar to how the Go code executes templates. For more complex scenarios, C# developers often use third-party templating engines or create custom solutions.

C#’s string interpolation (introduced in C# 6.0) provides a more readable way to embed expressions into string literals, which can be useful for simple templating scenarios.

Remember that while this code demonstrates similar concepts, C#’s approach to text templating is different from Go’s, and for more complex templating needs, you might want to consider using a dedicated templating library.