Does having lots of methods on a class increase the overhead of that class's object?

后端 未结 2 366
轻奢々
轻奢々 2021-01-04 03:33

Imagine I am using a class to bring back items from a database, say

class BankRecord {

public int id;
public int balance;

public void GetOverdraft() {
...
         


        
2条回答
  •  不知归路
    2021-01-04 03:57

    No. In the CLR objects have a pointer to that type's meta information which includes everything necessary for reflection and the method table. The overhead for looking up a method implementation in the method table is the same regardless of the number of entries in the table. See this article for more information. In a nutshell all objects have 8 bytes of overhead (4 for the type handle and 4 for the syncblock).

    However, what might be slower is reflection. It only makes since that if you want to enumerate through a type's metadata then it will be slower if there are more members declared in that type.

提交回复
热议问题