Is it expensive to create objects?

后端 未结 3 1186
小蘑菇
小蘑菇 2021-02-07 18:42

I have just refactored a colleague\'s code that, roughly, looked like this...

public class Utility
  public void AddHistoryEntry(int userID, HistoryType Historyty         


        
3条回答
  •  暖寄归人
    2021-02-07 19:21

    Why not a static method in the class itself?

    public static void AddHistoryEntry(HistoryEntry entry)

    Saves you the utility class. The new object may be expensive, it may not--but it probably doesn't matter. Good defaults allow you to extend your code much more easily, and you'll save more time in maintenance than you do in performance, unless you're doing this in a loop.

提交回复
热议问题