24

There is

///<summary>
///This is summary for some class or method
///</summary>

documentation for classes or methods. But how to write this for simple variables or lists?

I use Visual Studio 2010 and when i hover over some list, property or what ever i would like to see some kind of summary (in that little tooltip) i have written to that specific thing.

///<doc>
///always use this list!
List<String> beer = new List<String>();

edit: ok, we have found out, that it works as usual as long u comment in your class but OUTSIDE a method or a function!!

Any way to document/comment within a method too?

public class BeerForall
{
    /// <summary>
    /// it works here
    /// </summary>
    public List<String> beer = new List<string>();

    public String giveBeer()
    {
        /// is not working, u can not comment
        /// <summary>
        /// test test, not working
        /// </summary>
        List<String> moreBeer = new List<string>();

        return "beer";
    }
}
Gero
  • 12,993
  • 25
  • 65
  • 106

6 Answers6

19

As others mentioned, you can't get IntelliSense for local vars. However: If your function is so large that a "regular" comment is not close enough to read near the place where you're using the var, then the right fix is to refactor the function -- break it up into multiple, smaller methods, with fewer vars. I don't think this feature should exist, as it would serve only to facilitate writing excessively large functions.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • 3
    Maybe you're not seeing how that could be useful. I suggest you take a look at doxygen. In doxygen, whatever documentation you can write in a comment before the function you can write also inside the function. That allows you to continue for instance the remarks section with an explanation of the algorithm, interleaving code and documentation in a style which is very close to literal programming. Some pieces of code benefit a lot. – migle Mar 28 '17 at 10:59
  • 1
    Refactoring code is good but I don't see how taking tools away from the programmer is a useful "feature" for enforcing good coding practices. FWIW I don't think it's working, whoever wrote the codebase I'm maintaining now did not say "oh gee, I can't comment local variables so that means I'm going to code better now". Also, hypothetically if this was really left out to encourage a good coding style, why do local variable Intellisense comments work for C/C++ but not C# in VS2013? – jrh May 02 '17 at 21:10
  • 7
    "I don't think this feature should exist, as it would serve only to facilitate writing excessively large functions." I think it should exists unless you can rewrite 30k+ lines of code of backend I have to deal with. – Maciej Szpakowski Sep 14 '17 at 15:50
  • 5
    We are actually documenting these variables in order to refactor the mega methods in the 1st place and the lack of this feature is not helping... – Zar Shardan Nov 08 '17 at 20:03
  • A bit late, but... If you're documenting them JUST for a refactor, rename them. Variables with 30+ character names are a bad fit for published code, but in the middle of a refactor they are just fine. – Sparr Oct 14 '22 at 20:57
13

Seems to work perfectly fine in Visual Studio 2010. I defined a List as a private field with a comment inside my MainForm class.

They won't work for local variables defined within functions though.

enter image description here

gideon
  • 19,329
  • 11
  • 72
  • 113
  • in the class itself it works. but try to document something within a method. see my edited first post – Gero Oct 31 '11 at 13:14
  • 3
    Yea thats what I said in my first comment to your question, you can't document variables defined within a method itself. – gideon Oct 31 '11 at 13:19
  • 3
    woudl be cool if could make it work for local method-level variables.. just so you could have intellisense for your local code. I mean, why not? – Brady Moritz Oct 20 '12 at 22:16
6

As far as I know, adding comments for intellisense will not work for local variables declared within functions. If you were to make your local list an instance variable of the class, you would be able to do this.

ooPeanutButter
  • 433
  • 2
  • 9
0

Edit: it looks like <var> is only supported for JavaScript.

As of Visual Studio 2012 you can add this documentation using the <var> element.

Here's Microsoft's documentation on it:
https://msdn.microsoft.com/en-us/library/hh542722(v=vs.110).aspx

  • Unfortunately, the linked documentation only applies to Javascript. I tried using `` on a local variable as such, no effect. – John Weisz May 20 '16 at 13:31
0

The same way as you write summary for classes and methods work for variables.

Pedryk
  • 1,643
  • 1
  • 13
  • 28
0

You can add such XML documentation comments to any class member, not inside a member (method, property, etc).

abatishchev
  • 98,240
  • 88
  • 296
  • 433