Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the type parameter naming guidelines?

I noticed, as well as saw in the Essential C# 3.0 book, that paramters are usually defined as T or TEntity

For example:

public class Stack<T>
{


}

or

public class EntityCollection<TEntity>
{


}

How do you decide which name to use?

Thanks


2 Answers

I fetched the .NET Framework 4.6 source code from http://referencesource.microsoft.com/dotnet46.zip. Extracted it and processed the data to extract the generic parameter name from all generic class declarations.

Note: I only extracted the generic parameter name from generic classes with only one generic parameter. So this does not take into consideration the generic classes with multiple generic parameters.

grep -nohrP "class \w+<T\w*>" | sed -e 's/.*\<//' -e 's/>//' | sort | uniq -cd | sort -bgr

Result:

361 T
 74 TChannel
 51 TKey
 33 TResult
 30 TSource
 28 T_Identifier
 18 TElement
 12 TEntity
 11 TInputOutput
  7 TItem
  6 TLeftKey
  6 TFilterData
  5 T_Query
  4 T_Tile
  4 TInput
  3 TValue
  3 TRow
  3 TOutput
  3 TEventArgs
  3 TDataReader
  3 T1
  2 TWrapper
  2 TVertex
  2 TValidationResult
  2 TSyndicationItem
  2 TSyndicationFeed
  2 TServiceType
  2 TServiceModelExtensionElement
  2 TResultType
  2 TMessage
  2 TLocationValue
  2 TInnerChannel
  2 TextElementType
  2 TException
  2 TEnum
  2 TDuplexChannel
  2 TDelegate
  2 TData
  2 TContract
  2 TConfigurationElement
  2 TBinder
  2 TAttribute
like image 134
Fred Avatar answered Sep 09 '25 12:09

Fred


Here is my set of rules

  • If there is one parameter, I name it T
  • If there is more than one parameter, I pick a meaningful name and prefix with T. For example TKey, TValue

For a semi-official opinion, it's worth looking at the framework design guidelines on the subject:

  • http://blogs.msdn.com/brada/archive/2005/12/02/497340.aspx
like image 27
JaredPar Avatar answered Sep 09 '25 14:09

JaredPar