Should I avoid nested types in generic types?

Viewed 497

If I implement a generic type that risks being instantiated with lots of type params, should I avoid (for JIT performance/code size etc. reasons) having many nested non-generic types?

Example:

public class MyGenericType<TKey, TValue>
{
    private struct IndexThing
    {
        int row; int col;
    }

    private struct SomeOtherHelper
    {
        ..
    }

    private struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>> { }

}

The alternative which works equally well is to have the non-generic types outside, but then they pollute the namespace. Is there a best practice?

public class MyGenericType<TKey, TValue>
{
    private struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>> { }
}

internal struct IndexThingForMyGenericType
{
   int row; int col;
}

internal struct SomeOtherHelper
{
   ...
}
2 Answers
Related