I decided to ask this question here instead of on stack overflow because it is r
ID: 642843 • Letter: I
Question
I decided to ask this question here instead of on stack overflow because it is rather subjective.
In C#, typically I see generic types with very poor names. Specifically, "T" is commonly used but is not a meaningful name by itself. For example:
class Fruit<T>
{
T fruit;
}
While this is the typical approach, would anyone recommend against this? And if so, what would a reasonable naming convention be for generic types in the context of C# for generic functions and classes?
In my previous example, let's assume that generic type T must always be a type of fruit, such as Apple or Orange. The type T needs to make it obvious that it's a type of fruit, so maybe a better name would be FruitType, so we end up with:
class Fruit<FruitType>
{
FruitType fruit;
}
This is just to give you guys an idea of what I'm looking for. What's an acceptable "rule of thumb" for this issue?
Explanation / Answer
It is indeed subjective...ish.
As some folks find i is perfectly valid for a for loop variable, some think T is perfectly valid for a type place-holder in a generic class.
I personally espouse this approach, it's a common convention and people generally know what you mean.
Where the type is meaningful I'd use a meaningful name, but generally start it with T. I recently developed a generic dictionary class (don't ask) and the declaration was
public class Dictionary<TKey, TValue>
However, for something like a Tuple, where the types are essentially meaningless, I consider the following perfectly acceptable.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.