0

I've tried a few things like Modifiers?.Select and String.Join amongst some other things. "Modifiers" here is a list of objects that have an int called Level and an enum for the Id. It's for a game that I'm fiddling around with in Unity.

The following code works fine as long as the list isn't empty, which it frequently will be.

Also - for reference, I got the structure for this line from this question: String.Join on a list of objects

Edit I forgot to mention that the error I'm getting is ArgumentNullException: Value cannot be null. If the list is empty.

public int Hash => $"{Id}.{String.Concat(Modifiers.Select(i => i.Id))}".GetHashCode();
  • That should work for an empty list, but for `null` you'd want to do something like `?? new string[0]` after it. – juharr Feb 04 '22 at 17:44
  • I tried this a few ways and I'm still struggling. Maybe I'm putting it in the wrong place? public int Hash => $"{Id}.{string.Concat(Modifiers.Select(i => i.Id))?? new string[0]}".GetHashCode(); This gives me type mismatch (string and string[]). I see what you're getting at with it though, I tried a few other things after new and it feels like I'm getting closer. – Mike Draper Feb 04 '22 at 18:26
  • Actually it is not entirely clear how you hold your objects in list... I create my custom class with enum id field and all work pretty fine with empty list. Can you give us implementation of `Modifiers`. – Claus Stolz Feb 05 '22 at 12:28

0 Answers0