Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commenting Generics - Is it possible to refer to generic type parameter not as T but as its actual type?

Given a method like this:

/// <summary>
/// Given a <see cref="T1"/>, return a <see cref="T2"/>.
/// </summary>
public T2 ExampleMethod(T1 t1)
{
   // omitted
}

can I write a comment such that Visual Studio will correctly display it, if T1 is an int and T2 is a string, for example? Or, am I stuck with T1 and T2 appearing in the comment?

Said differently: is there something I can do so that in Visual Studio, the tooltip on this method shows the actual type names?

Edit: I seem to have not explained what I'm actually interested in knowing.

Suppose I have a class called Widget<T1,T2>. Suppose I have a method like the one above.

Then, when I do

Widget<int,string> myExample = new Widget<int,string>();

myExample.ExampleMethod(... );  // HERE - if I mouse over the method  

// I get a tooltip. Is there something I can do to the comment in my 
// example method above that will allow me to say 
// "Given a System.Int32, return a System.String"?

Edit 2: Someone commented here a moment ago but their comment was deleted. They suggested I use

///Given a <see cref="T1"/>, return a <see cref="T2"/>.

Which is what I'm using now. This is fine. My question, phrased differently for the third time now, is Visual Studio smart enough to infer what T1 and T2 are from a constructor? I can figure out what T1 and T2 are from the method signature, and I thought maybe Visual Studio could do the same and support this in the comment.

My gut feeling is no, but hence the question.

like image 953
Charlie Salts Avatar asked Jan 16 '15 20:01

Charlie Salts


People also ask

How do you determine what type a generic parameter T is?

You can get the Type that represents T , and use the IsInterface property: Type type = typeof(T); if (type. IsInterface) { ... } If you want to know which interface is passed, just use == to compare the Type objects, e.g.

Which types can be generic parameters?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.

Can you have a generic method in a non-generic class?

Generic methods in non-generic classYes, you can define a generic method in a non-generic class in Java.

What is generic type arguments?

The generic argument list is a comma-separated list of type arguments. A type argument is the name of an actual concrete type that replaces a corresponding type parameter in the generic parameter clause of a generic type. The result is a specialized version of that generic type.


2 Answers

To supplement @Martin Mulder's answer as well as a couple of the comments for this question, I have submitted a problem report to the Visual Studio developer community to fix this "issue" as a feature request. I have basically requested that the Intellisense, when hovering over a statement that substitutes a generic for a specific type, shows the specific type in the comments when using the <typeparamref name=""/> tag in the comments. I am not sure if doing the same when using <see cref=""/> is appropriate, but if you feel like it is, feel free to add a comment to the problem report to request its inclusion in a fix!

Substitute Generics For Actual Type In Intellisense For XML Comments

like image 132
Blair Allen Avatar answered Oct 30 '22 12:10

Blair Allen


No, Visual Studio does not support that. You should ask yourself why you would want to use the specific type in your comments.

A good method documentation is done by describing it function in non-technical terms. And describe its parameters by using clear names and a good description what is expected of that parameter. The type is hardly ever need. If you do need it, you probably are trying to document your methods in a thechnical way, which is hardly ever a good idea.

like image 40
Martin Mulder Avatar answered Oct 30 '22 13:10

Martin Mulder