Dictionary with integer array as a key
It isn't predefined because it is expensive. If you know your list is short then just implement the obvious overrides. If not, you'll have to come up with some kind of heuristic for at least GetHashCode. Say, GetHashCode of only the first couple of elements xor-ed together with the Length.
How to map an array of integers as keys to dictionary values
You can map()
each array element to the corresponding value in
the dictionary. If it is guaranteed that all array elements are
present as keys in the dictionary then you can do:
let mysteryDoors = [3, 1, 2]
let doorPrizes = [ 1:"Car", 2: "Vacation", 3: "Gift card"]
let prizes = mysteryDoors.map { doorPrizes[$0]! }
print(prizes) // ["Gift card", "Car", "Vacation"]
To avoid a runtime crash if a key is not present, use
let prizes = mysteryDoors.flatMap { doorPrizes[$0] }
to ignore unknown keys, or
let prizes = mysteryDoors.map { doorPrizes[$0] ?? "" }
to map unknown keys to a default string.
How to use int array as dictionary key C#
Arrays are reference types. Thus, two arrays having the same contents have two different object identities and are not "equal".
If all your arrays have a constant number of elements (for example, if you use the array to store 2D coordinates), consider using a value tuple instead:
public Dictionary<(int, int), car> listOfCells = new Dictionary<(int, int), car>();
Whether a Dictionary can have Array as Key?
By default only references of the arrays would be compared, so you either have to
- provide a custom
IEqualityComparer<string[]>
or - use a
Tuple<string, string>
as key instead ( since you only have two strings)
Here's a similar question's answer which shows how to create a custom comparer for the Dictionary
- constructor.
Int array as a key in a Dictionary VS a string
As ever, you should test performance rather than guessing about it or asking others to guess. However, two things to think about:
- You've shown an IPv4 address... what about IPv6? That could definitely complicate things
- The IPv4 address is really 4 bytes... so why not convert that into a single
uint
orIPAddress
as the key? That way you don't need to write your ownEqualityComparer<int[]>
. It's a shameIPAddress
doesn't implementIEquatable<IPAddress>
, but it looks like it does overrideEquals
/GetHashCode
appropriately.
I'd consider the first point, write detailed benchmarks which work out how important this really is - if you're really only looking up about once per second, it's very unlikely to be important at all - and then try the various options. I would only move away from the simplest code (which would be a string if that's how you're already receiving the IP address) if you can show a proven, significant improvement.
Getting from a dictionary with int[] arrays as value
Create an IEqualityComparer
and define your dictionary using that. you can find sample code in below SO question:
An integer array as a key for Dictionary
Related Topics
The 'Await' Operator Can Only Be Used Within an Async Lambda Expression
Getting Relative Virtual Path from Physical Path
Regular Expression to Get the Src of Images in C#
How to Implement One "Catch'Em All" Exception Handler with Resume
Httpwebrequest Times Out on Second Call
Improving/Fixing a Regex for C Style Block Comments
Split String into Smaller Strings by Length Variable
How to Know the Repeating Decimal in a Fraction
Setting the Datasource for a Local Report - .Net & Report Viewer
How to Add My New User Control to the Toolbox or a New Winform