RecursiveCall
RecursiveCall

Reputation: 492

Using a private class in place of a field - performance/memory penalty?

I'm reviewing someone's code and came across this private class:

class CustomType : Dictionary<int, SomeOtherCustomType>
{
    // This is empty; nothing omitted here
}

CustomType is then used all over the parent class. This is neat of course, since CustomType is shorter than

Dictionary<int, SomeOtherCustomType>

My question is, what are the performance/memory implications of having an inner class just for a shortcut? In a performance sensitive application, does this contribute (even slightly) to higher memory and/or CPU usage?

Upvotes: 28

Views: 716

Answers (2)

Doctor Jones
Doctor Jones

Reputation: 21664

Unless there's another reason for defining a custom type I'd suggest changing it to a using statement.

using CustomType = Dictionary<int, SomeOtherCustomType>;

It just defines an alias for the dictionary and can be quite useful if you're consuming some complicated parameterised classes.

This eliminates the need of declaring a new type, because as the code stands at the moment the following would fail.

CustomType dictionary = new Dictionary<int, SomeOtherCustomType>(); //custom type is a different class and can't be instantiated with a dictionary

However, it would work if you use an alias instead.

Upvotes: 50

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

The short and the long of it is, no. The class CustomType is by definition a Dictionary<int, SomeOtherCustomType> and so it will be allocated as such.

This is particularly true because there literally is no implementation for the class.

Upvotes: 15

Related Questions