How do I escape characters in C# comments?

C#

C# Problem Overview


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 &lt; and &gt;? 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.

C# Solutions


Solution 1 - C#

If you need to escape characters in XML comments, you need to use the character entities, so < would need to be escaped as &lt;, 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 - C#

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>

Solution 3 - C#

You said "I want to make it easy to read the comment in the actual document". I agree.

Developers spend most of their lives in the code, not perusing auto-generated docs. Those are great for thirdparty libraries like charting, but not for in-house development where we work with all of the code. I'm sort of shocked that MSFT hasn't come up with a solution that supports developers better here. We have regions that dynamically expand/collapse code...why can't we have an in-place comment rendering toggle (between raw text and processed XML comment or between raw text and processed HTML comment)?. Seems like I should have some elementary HTML capabilities in my method/class prologue comments (red text, italics, etc). Surely an IDE could work a little HTML processing magic to liven up inline comments.

My hack-of-a-solution solution: I change '<' to "{" and '>" to "}". That seems to cover me for the typical example usage style comment, including your specific example. Imperfect, but pragmatic given the readability issue (and problems with IDE comment coloring that ensue when using '<')

Solution 4 - C#

C# XML comments are written in XML, so you would use normal XML escaping.

For example...

<summary>Here is an escaped &lt;token&gt;</summary>

Solution 5 - C#

I've found a livable solution to this problem is simply including two examples: one difficult-to-read version in the XML comments w/ escape characters, and another readable version using conventional // comments.

Simple, but effective.

Solution 6 - C#

Better than using {...} is using ≤...≥ (less than or equals sign, greater than or equals sign, U2264 and U2265 in Unicode). Looks like underlined angle brackets but still definitely angle brackets! And only adds a couple of bytes to your code file.

Solution 7 - C#

Even better try U2280 and U2281 - just copy and paste from List of Unicode characters (mathematical operators section).

Solution 8 - C#

I like @Mick Bruno's idea of using similar looking characters to keep it legible in the code. Single guillemets work pretty well:

List‹int›

The Windows keyboard shortcuts are:

  • Alt+0139
  • Alt+0155

(where the numbers are typed on the keypad)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomas JanssonView Question on Stackoverflow
Solution 1 - C#OdedView Answer on Stackoverflow
Solution 2 - C#Mark PimView Answer on Stackoverflow
Solution 3 - C#DaveView Answer on Stackoverflow
Solution 4 - C#Bob BlackView Answer on Stackoverflow
Solution 5 - C#HolySamosaView Answer on Stackoverflow
Solution 6 - C#Mick BrunoView Answer on Stackoverflow
Solution 7 - C#Paul CoulsonView Answer on Stackoverflow
Solution 8 - C#GilesView Answer on Stackoverflow