简体   繁体   中英

Does c# have inner class extra overhead?

I have the following simple class:

class Stack {
  public class Node // inner class
  {
      string item;
      Node next;
  }
}

A stack with N items uses: 8 bytes (reference to String) + 8 bytes (reference to Node) + 16 bytes (sync block index + type object pointer) . But I wonder about inner class overhead. Do I need to add an extra 8 bytes ? So a stack with N items uses ~ 40*N bytes or ~32*N bytes?

C# inner classes do not have a hidden reference to an instance of the outer class like Java does it. If you want that behavior you can create it manually. There is nothing like that in C#.

An inner class mainly has different access rules and a differently structured name. Also, inner classes share the generic type parameters of the outer class which might create overhead. It's mainly an organization concept (in C#).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM