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.
Usage Example
Section titled “Usage Example”using System;using System.Collections.Generic;
// Convention: T for a single typepublic class Transformer<T>{ public T Transform(T input) { return input; }}
// Convention: TKey and TValue for a dictionarypublic 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")); }}Output console
Section titled “Output console”dotnet run30Important notes
Section titled “Important notes”Tis the most common name for a single type parameter.TKeyandTValueare used for key-value collections.- Avoid names like
T1,T2except for trivial cases.
Real-world usage example
Section titled “Real-world usage example”Dictionary<TKey, TValue> – Uses exactly this convention. List<T> uses T.
See .NET docs on naming conventions.