-1

I want to implement two methods: AddPlayers and AddPlayer. Is it better when AddPlayers calls AddPlayer or AddPlayer calls AddPlayers with a single item array? Is there a significant difference in performance and IL output?

// AddPlayers contains the logic
void AddPlayer(Player player)
{
    return AddPlayers(new [] { player });
}

vs

// AddPlayer contains the logic
void AddPlayers(IEnumerable<Player> players)
{
    foreach (var player in players)
    {
        AddPlayer(player);
    } 
}
Konrad
  • 1,569

1 Answers1

4

I'd ask 2 questions:

  1. Which choice results in more readable code?
  2. Will either choice give you an obvious optimization opportunity, based on expected use cases?

    Gnat's right in his comment about micro-optimization but if you're adding 100,000 players at once, you're going to want the logic in AddPlayers (with a bulk insert). You'll also want a comment in AddPlayer warning the developer not to call it in a loop.

In a more typical situation (adding perhaps at most a dozen players at a time) then it likely doesn't matter much which option you choose.

Dan Pichelman
  • 13,853