How do I escape characters in C# comments?
I realized today that I don't know how to escape characters in comments for C#. I want to document a generic C# class, but I cannot write a proper example since I don't know how to escape the <
and >
characters. Do I have to use <
and >
? I don't like if that is the case since I want to make it easy to read the comment in the actual document so I don't have to generate some kind of code document to be able to read the example code.
Solution 1:
If you need to escape characters in XML comments, you need to use the character entities, so <
would need to be escaped as <
, as in your question.
The alternative to escaping is using CDATA
sections, to the same effect.
As you noted, this would produce good looking documentation, but a horrible comment to read...
Solution 2:
In plain C# comments you can use any character (except */
if you started the comment with /*
, or the newline character if you started the comment with //
). If you are using XML comments then you can use a CDATA section to include '<' and '>' characters.
See this MSDN blog article for more information on XML comments in C#.
For example
/// <summary>
/// Here is how to use the class: <![CDATA[ <test>Data</test> ]]>
/// </summary>