Skip to content

Type parameters naming (T, TKey, TValue)

Context: By convention, type parameters are named with a prefix T (for Type). For a single parameter, use T. For multiple parameters, use descriptive names like TKey, TValue, TInput, TOutput. This improves code readability.

using System;
using System.Collections.Generic;
// Convention: T for a single type
public class Transformer<T>
{
public T Transform(T input)
{
return input;
}
}
// Convention: TKey and TValue for a dictionary
public class SimpleDictionary<TKey, TValue>
{
private Dictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();
public void Add(TKey key, TValue value)
{
_dictionary.Add(key, value);
}
public TValue Get(TKey key)
{
return _dictionary[key];
}
}
class Program
{
static void Main()
{
var dict = new SimpleDictionary<string, int>();
dict.Add("age", 30);
Console.WriteLine(dict.Get("age"));
}
}
Terminal window
dotnet run
30
  • T is the most common name for a single type parameter.
  • TKey and TValue are used for key-value collections.
  • Avoid names like T1, T2 except for trivial cases.

Dictionary<TKey, TValue> – Uses exactly this convention. List<T> uses T.
See .NET docs on naming conventions.