C# – Formatting Code for Wiki Syntax

markdownmediawikiwiki

How can I format my C# code so it displays correctly in our internal Wiki?

We are using MediaWiki although I assume most wiki syntax is the same.

Best Answer

If you are using the GeShi syntax highlighting extension to MediaWiki, then you can just insert your C♯ code as-is, enclosed within a <syntaxhighlight/> element. (This is, as you can see from the syntax highlighting extensions category, one of several such MediaWiki extensions.)

The MediaWiki wikis used, by the Wikimedia Foundation, for Wikipedia, Wiktionary, Wikibooks, Wikiquote, et al. all have this extension enabled. Here is some wikitext taken directly from the current (at the time of writing) Wikipedia article for C♯ (q.v.) that exemplifies what the wikitext looks like:

<syntaxhighlight lang=CSharp>
// Declare the generic class.

public class GenericList<T>
{
    void Add(T input) { }
}

class TestGenericList
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int.
        GenericList<int> list1 = new GenericList<int>();

        // Declare a list of type string.
        GenericList<string> list2 = new GenericList<string>();

        // Declare a list of type ExampleClass.
        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
    }
}
</syntaxhighlight>

See the Wikipedia article for how this wikitext is rendered.