SingleTypeParameter

SingleTypeParameter

Summary

Use T as a type parameter name if the type parameter is single.

Default severity

Warning

Description

Names of Classes, Structs, and Interfaces [1] is quoted as follows:

Names of Generic Type Parameters

  • ✓ Consider using T as the type parameter name for types with one single-letter type parameter.

However, the following cases are excluded because renaming to T may cause a compile error or change the meaning:

  • The type name is T and it has one type parameter
  • The type T is already contained in the type or member

Code fix

The code fix provides an option replacing the type parameter name with T.

Example

Diagnostic

public sealed class Code<Type>
{
    public Code(Type instance)
    {
        Instance = instance;
    }

    public Type Instance { get; }
}

Code fix

public sealed class Code<T>
{
    public Code(T instance)
    {
        Instance = instance;
    }

    public T Instance { get; }
}

References

[1] Microsoft, .NET Framework Design Guidelines